|
| 1 | +--- |
| 2 | +title: "Sample: Blazor WebAssembly with Global Discovery (C#) (Microsoft Dataverse) | Microsoft Docs" # Intent and product brand in a unique string of 43-59 chars including spaces |
| 3 | +description: "This sample a Blazor WebAssembly project that uses the Global Discovery Service and Dataverse Web API to access multiple environments available to the user." # 115-145 characters including spaces. This abstract displays in the search result. |
| 4 | +ms.date: 07/18/2022 |
| 5 | +author: ImadYanni |
| 6 | +ms.author: iyanni |
| 7 | +ms.reviewer: jdaly |
| 8 | +search.audienceType: |
| 9 | + - developer |
| 10 | +search.app: |
| 11 | + - PowerApps |
| 12 | + - D365CE |
| 13 | +contributors: |
| 14 | + - JimDaly |
| 15 | + - phecke |
| 16 | +--- |
| 17 | +# Sample: Blazor WebAssembly with Global Discovery |
| 18 | + |
| 19 | +This sample shows how to use the Global Discovery service from a Blazor WebAssembly application. |
| 20 | + |
| 21 | +You can download the sample from [https://github.com/microsoft/PowerApps-Samples/tree/master/cds/webapi/C#/BlazorCDS](https://github.com/microsoft/PowerApps-Samples/tree/master/cds/webapi/C%23/BlazorCDS) |
| 22 | + |
| 23 | +This sample is a modification of the steps in the [Tutorial: Create an ASP.NET Core Blazor WebAssembly app using Microsoft Dataverse](walkthrough-blazor-webassembly-single-tenant.md) |
| 24 | + |
| 25 | +## How to run this sample |
| 26 | + |
| 27 | +1. Download or clone the [Samples](https://github.com/microsoft/PowerApps-Samples) repo so that you have a local copy. |
| 28 | +1. Follow the instructions in [Setup](#setup) to create an Azure Active Directory app registration and run the sample. |
| 29 | + |
| 30 | +## What this sample does |
| 31 | + |
| 32 | +This sample demonstrates how to provide a select (drop-down) control in a web application to allow users to switch environments that they have access to. |
| 33 | + |
| 34 | +## How this sample works |
| 35 | + |
| 36 | +The `Pages/EnvironmentChooser.razor `file provides the UI to allow users to select from available environments based on their credentials after they have logged in. |
| 37 | + |
| 38 | +This page depends on a named definition of an HttpClient (`GDSClient`) in Program.cs that is configured to access the Global Discovery Service. Access to the Global Discovery Service is added to the available scopes when the user logs in. When the user selects a different environment, the `SelectedEnvUrl` in `Models/AppState.cs` changes. |
| 39 | + |
| 40 | +Access to the account records is provided by `Pages/FetchAccounts.razor` with an access token that depends on the selected environment. |
| 41 | + |
| 42 | +### Setup |
| 43 | + |
| 44 | +To run this sample you must first configure an Azure Active Directory application on your tenant and update the `BlazorCDS\wwwroot\appsettings.json` file to replace the placeholder `ClientId` value with the application (client) Id. Use the following steps: |
| 45 | + |
| 46 | +#### Create an app registration |
| 47 | + |
| 48 | +1. Go to [Azure](https://portal.azure.com/). |
| 49 | +1. Select **Azure Active Directory**. |
| 50 | +1. Go To **App registrations**. |
| 51 | +1. Select **New Registration**. |
| 52 | +1. Enter the following: |
| 53 | + |
| 54 | + |Field|Value| |
| 55 | + |---------|---------| |
| 56 | + |Name|Your choice. You might use: `BlazorGlobalDiscoverySample`| |
| 57 | + |Supported account types:|Accounts in this organization directory only (Single Tenant)| |
| 58 | + |Redirect URI (optional)|`https://localhost:44363/authentication/login-callback`| |
| 59 | + |
| 60 | +1. Click **Register**. |
| 61 | +1. Copy the **Application (client) ID** value. You will need this when you [Update the project](#update-the-project). |
| 62 | + |
| 63 | +#### Configure Authentication |
| 64 | + |
| 65 | +1. Go to **Authentication**. |
| 66 | +1. Under **Implicit grant and hybrid flows**, select both of the following options: |
| 67 | + |
| 68 | + - Access tokens (used for implicit flows) |
| 69 | + - ID tokens (used for implicit and hybrid flows) |
| 70 | + |
| 71 | +1. Click **Save**. |
| 72 | + |
| 73 | +#### Configure Permissions |
| 74 | + |
| 75 | +1. Go to **API permissions**. |
| 76 | +1. Click **Add a permission**. |
| 77 | +1. In the **Request API permissions** fly-out, under the**Microsoft APIs** tab, select **Dynamics CRM**. |
| 78 | +1. Under **Select permissions**, select **user_impersonation**. |
| 79 | +1. Click **Add permissions**. |
| 80 | +1. Under **Configured permissions**, click **Grant admin consent** for {Your tenant name.} |
| 81 | +1. In the **Grant admin consent** confirmation dialog, click **Yes**. |
| 82 | + |
| 83 | +#### Update the project |
| 84 | + |
| 85 | +1. Open the BlazorCDS Visual Studio solution file (BlazorCDS.sln) with Visual Studio |
| 86 | +1. In **Solution Explorer**, expand `wwwroot` and open the `appsettings.json` file. |
| 87 | +1. Replace the placeholder `ClientId` value (`11111111-1111-1111-1111-111111111111`) with the application (client) ID value you copied in [Create an app registration](#create-an-app-registration). |
| 88 | + |
| 89 | +#### Run the sample |
| 90 | + |
| 91 | +In Visual Studio, click **IIS Express** to run the sample. |
| 92 | + |
| 93 | +### Demonstrate |
| 94 | + |
| 95 | +1. In the browser window that opens with the app, click **Log in**. |
| 96 | +1. Enter your credentials and you will see a select control with the label: **Choose an Environment**. The select control should list all the environments you have access to with the credentials you provided. |
| 97 | +1. Click **Fetch Accounts**. This should display a list of account records you have access to view in the selected environment. |
| 98 | +1. If you have more than one environment to choose from, select a different environment and the list of account records will be refreshed to represent the records available in that environment. |
| 99 | + |
| 100 | +### Clean up |
| 101 | + |
| 102 | +This sample makes no changes to data in your environment. |
| 103 | + |
| 104 | + |
| 105 | +[!INCLUDE [footer-banner](../../includes/footer-banner.md)] |
0 commit comments