Skip to content

Latest commit

 

History

History
 
 
page_type languages products name urlFragment description
sample
csharp
aspnet-core
azure-active-directory
azure-web-apps
Enable your Blazor Server to sign-in users with the Microsoft identity platform
ms-identity-blazor-server
This sample demonstrates an ASP.NET Core Blazor Server application that authenticates users with Azure AD

Enable your Blazor Server to sign-in users with the Microsoft identity platform

  1. Overview
  2. Scenario
  3. Contents
  4. Prerequisites
  5. Setup
  6. Registration
  7. Running the sample
  8. Explore the sample
  9. About the code
  10. Next chapter of the tutorial: the Web app calls Microsoft Graph
  11. More information
  12. Community Help and Support
  13. Contributing
  14. Code of Conduct

.NET Core

Overview

This sample demonstrates an ASP.NET Core Blazor Server application that authenticates users with the Microsoft Identity Platform.

Scenario

  1. The client ASP.NET Core Blazor Server application uses the Microsoft Authentication Library MSAL.NET and Microsoft.Identity.Web libraries to sign-in a user in their tenant and obtain an ID Tokens from Azure AD.
  2. The ID Token proves that the user has successfully authenticated against Azure AD.

Overview

Prerequisites

Setup

In the downloaded folder

From your shell or command line:

cd ms-identity-blazor-server\WebApp-OIDC\MyOrg

⚠️ To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive.

Register the sample application(s) with your Azure Active Directory tenant

There is one project in this sample. To register it, you can:

  • follow the steps below for manually register your apps
  • or use PowerShell scripts that:
    • automatically creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you.
    • modify the projects' configuration files.
Expand this section if you want to use this automation:

⚠️ If you have never used Azure AD Powershell before, we recommend you go through the App Creation Scripts once to ensure that your environment is prepared correctly for this step.

  1. On Windows, run PowerShell as Administrator and navigate to the root of the cloned directory

  2. In PowerShell run:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
  3. Run the script to create your Azure AD application and configure the code of the sample application accordingly.

  4. In PowerShell run:

    cd .\AppCreationScripts\
    .\Configure.ps1

    Other ways of running the scripts are described in App Creation Scripts The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.

Choose the Azure AD tenant where you want to create your applications

As a first step you'll need to:

  1. Sign in to the Azure portal.
  2. If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Azure AD tenant.

Register the webApp app (WebApp-blazor-server)

  1. Navigate to the Azure portal and select the Azure AD service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. In the Register an application page that appears, enter your application's registration information:
    • In the Name section, enter a meaningful application name that will be displayed to users of the app, for example WebApp-blazor-server.
    • Under Supported account types, select Accounts in this organizational directory only.
    • In the Redirect URI (optional) section, select Web in the combo-box and enter the following redirect URI: https://localhost:44318/.

      Note that there are more than one redirect URIs used in this sample. You'll need to add them from the Authentication tab later after the app has been created successfully.

  4. Select Register to create the application.
  5. In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
  6. In the app's registration screen, select Authentication in the menu.
    • If you don't have a platform added, select Add a platform and select the Web option.
    • In the Redirect URIs section, enter the following redirect URIs.
      • https://localhost:44318/signin-oidc
    • In the Implicit grant section, check the ID tokens option.
    • In the Logout URL section, set it to https://localhost:44318/signout-oidc.
  7. Select Save to save your changes.

Configure the webApp app (WebApp-blazor-server) to use your app registration

Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.

In the steps below, "ClientID" is the same as "Application ID" or "AppId".

  1. Open the appsettings.json file.
  2. Find the key ClientId and replace the existing value with the application ID (clientId) of the WebApp-blazor-server application copied from the Azure portal.
  3. Find the key TenantId and replace the existing value with your Azure AD tenant ID.
  4. Find the key Domain and replace the existing value with your Azure AD tenant name.

Running the sample

You can run the sample by using either Visual Studio or command line interface as shown below:

Run the sample using Visual Studio

Clean the solution, rebuild the solution, and run it.

Run the sample using a command line interface such as VS Code integrated terminal

Step 1. Install .NET Core dependencies

cd blazorserver-singleOrg
dotnet restore

Step 2. Trust development certificates

dotnet dev-certs https --clean
dotnet dev-certs https --trust

Learn more about HTTPS in .NET Core.

Step 3. Run the applications

In the console window execute the below command:

dotnet run

Explore the sample

  1. Open your browser and navigate to https://localhost:44318.

  2. Select the Sign in button on the top right corner. When the user signs-in for the first time , a consent screen is presented with required permissions, select Accept.

    You will see claims from the signed-in user's token.

UserClaims

ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.

We'd love your feedback!

Were we successful in addressing your learning objective? Do consider taking a moment to share your experience with us.

How was the code created

  1. Run the following command to download the templates for Microsoft.Identity.Web

    dotnet new -i Microsoft.Identity.Web.ProjectTemplates::1.1.0
  2. Create the application by running the following command:

    dotnet new blazorserver2 --auth SingleOrg
  3. Add UserClaims.razor component and UserClaimsBase.cs class.

About the code

  1. In Startup.cs, add below lines of code in ConfigureServices method:

    services.AddMicrosoftIdentityWebAppAuthentication(Configuration);

    This enables your application to use the Microsoft identity platform endpoint to sign-in users.

  2. Index.razor is the landing page when application starts. Index.razor contains child component called UserClaims. If user is authenticated successfully, UserClaims displays a few claims present in the ID Token issued by Azure AD.

  3. In the UserClaimsBase.cs class, GetClaimsPrincipalData method retrieves signed-in user's claims using the GetAuthenticationStateAsync() method of the AuthenticationStateProvider class.

    public class UserClaimsBase : ComponentBase
    {
       [Inject]
       private AuthenticationStateProvider AuthenticationStateProvider { get; set; }
       protected string _authMessage;
       protected IEnumerable<Claim> _claims = Enumerable.Empty<Claim>();
       private string[] printClaims = { "name", "preferred_username", "tid", "oid" };
       protected override async Task OnInitializedAsync()
       {
           await GetClaimsPrincipalData();
       }
       private async Task GetClaimsPrincipalData()
       {
           var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
           var user = authState.User;
           if (user.Identity.IsAuthenticated)
           {
               _authMessage = $"{user.Identity.Name} is authenticated.";
               _claims = user.Claims.Where(x => printClaims.Contains(x.Type));
           }
           else
           {
               _authMessage = "The user is NOT authenticated.";
           }
       }
    }

Next chapter of the tutorial: the Web app calls Microsoft Graph

In the next chapter, we will enhance this Web app to call downstream Web API (Microsoft Graph).

See Call-MSGraph

More information

For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.

Community Help and Support

Use Stack Overflow to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [azure-active-directory azure-ad-b2c ms-identity msal].

If you find a bug in the sample, raise the issue on GitHub Issues.

To provide feedback on or suggest features for Azure Active Directory, visit User Voice page.

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.