Muokkaa

Jaa


Sample: Blazor WebAssembly with Global Discovery

This sample shows how to use the Global Discovery service from a Blazor WebAssembly application.

You can download the sample from https://github.com/microsoft/PowerApps-Samples/tree/master/dataverse/webapi/C#/BlazorCDS

This sample is a modification of the steps in the Tutorial: Create an ASP.NET Core Blazor WebAssembly app using Microsoft Dataverse

You can try this sample online here: https://blazorcds.mohsinonxrm.com/

How to run this sample

  1. Download or clone the Samples repo so that you have a local copy.
  2. Follow the instructions in Setup to create an Microsoft Entra ID app registration and run the sample.

What this sample does

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.

How this sample works

The Pages/EnvironmentChooser.razor file provides the user interface to allow users to select from available environments based on their credentials after they have logged in.

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.

Access to the account records is provided by Pages/FetchAccounts.razor with an access token that depends on the selected environment.

Setup

To run this sample you must first configure an Microsoft Entra ID 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:

Create an app registration

  1. Go to Azure.

  2. Select Microsoft Entra ID.

  3. Go To App registrations.

  4. Select New Registration.

  5. Enter the following:

    Field Value
    Name Your choice. You might use: BlazorGlobalDiscoverySample
    Supported account types: Accounts in this organization directory only (Single Tenant)
    Redirect URI (optional) https://localhost:44363/authentication/login-callback
  6. Click Register.

  7. Copy the Application (client) ID value. You will need this when you Update the project.

Configure Authentication

  1. Go to Authentication.

  2. Under Implicit grant and hybrid flows, select both of the following options:

    • Access tokens (used for implicit flows)
    • ID tokens (used for implicit and hybrid flows)
  3. Click Save.

Read the following important information about using implicit grant or implicit flow in application code.

Important

Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this article requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.

Configure Permissions

  1. Go to API permissions.
  2. Click Add a permission.
  3. In the Request API permissions fly-out, under theMicrosoft APIs tab, select Dynamics CRM.
  4. Under Select permissions, select user_impersonation.
  5. Click Add permissions.
  6. Under Configured permissions, click Grant admin consent for {Your tenant name.}
  7. In the Grant admin consent confirmation dialog, click Yes.

Update the project

  1. Open the BlazorCDS Visual Studio solution file (BlazorCDS.sln) with Visual Studio.
  2. In Solution Explorer, expand wwwroot and open the appsettings.json file.
  3. Replace the placeholder ClientId value (11111111-1111-1111-1111-111111111111) with the application (client) ID value you copied in Create an app registration.

Run the sample

In Visual Studio, click IIS Express to run the sample.

Demonstrate

  1. In the browser window that opens with the app, click Log in.
  2. 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.
  3. Click Fetch Accounts. This should display a list of account records you have access to view in the selected environment.
  4. 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.

Clean up

This sample makes no changes to data in your environment.

See Also

Discover user organizations
Sample: Global Discovery Service (C#)