services | platforms | author | level | client | endpoint |
---|---|---|---|---|---|
active-directory |
dotnet |
negoe |
100 |
ASP.NET Core Web App |
Microsoft identity platform |
Build an ASP.NET Core Web app signing-in users in sovereign clouds with the Microsoft identity platform
This sample shows how to build a .NET Core MVC Web app that uses OpenID Connect to sign in users. Users can only sign in with their 'work and school' accounts in their organization belonging to national or sovereign clouds. This sample use US Government cloud scenario. It leverages the ASP.NET Core OpenID Connect middleware.
National clouds (aka Sovereign clouds) are physically isolated instances of Azure. These regions of Azure are designed to make sure that data residency, sovereignty, and compliance requirements are honored within geographical boundaries.
In addition to the public cloud, Azure Active Directory is deployed in the following National clouds:
- Azure US Government
- Azure China 21Vianet
- Azure Germany
Note that enabling your application for sovereign clouds requires you to:
- register your application in a specific portal, depending on the cloud
- use a specific authority, depending on the cloud in the config file for your application
- in case you want to call the graph, this requires a specific Graph endpoint URL, depending on the cloud.
More details in Authentication in National Clouds
To run this sample:
Pre-requisites: Install .NET Core 3.0 or later (for example for Windows) by following the instructions at .NET and C# - Get Started in 10 Minutes. In addition to developing on Windows, you can develop on [Linux](https://ww w.microsoft.com/net/core#linuxredhat), Mac, or Docker.
This sample was created from the dotnet core 3.0 dotnet new mvc template with SingleOrg
authentication, and then tweaked to let it support tokens for the Microsoft identity platform endpoint.
You can clone this sample from your shell or command line:
git clone https://github.com/Azure-Samples/microsoft-identity-platform-aspnetcore-webapp-tutorial webapp
cd webapp
cd "1-WebApp-OIDC\1-4-Sovereign"
Given that the name of the sample is very long, and so are the name of the referenced NuGet packages, you might want to clone it in a folder close to the root of your hard drive, to avoid file size limitations on Windows.
-
Sign in to the US Government Azure portal
For registering your app in other National Clouds go to App Registration endpoints of the National Cloud of your choice using either a work or school account.
Note: Azure Germany doesn't support *App registrations (Preview) experience.
-
Choose the Azure AD tenant where you want to create your applications
- If your account is present in more than one Azure AD tenant, select profile button at the top-right corner in the menu on top of the page and select
Switch Directory
. - On
Directory + Subscription
switch your portal session to the desired Azure AD tenant.
- If your account is present in more than one Azure AD tenant, select profile button at the top-right corner in the menu on top of the page and select
-
In the left-hand navigation pane, select the Azure Active Directory service, and then select App registrations (Preview).
-
In App registrations (Preview) page, select New registration.
-
When the Register an application page 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
. - In the Supported account types section, select Accounts in this organizational directory only (name of your org).
- In the Redirect URI (optional) section, select Web in the combo-box.
- For the Redirect URI, enter the base URL for the sample. By default, this sample uses
https://localhost:44321/
. - Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
-
On the app Overview page, find the Application (client) ID value and record it for later. You'll need it to configure the Visual Studio configuration file for this project.
-
Still in the Overview page, find the Directory (tenant) ID value and record it for later.
-
In the list of pages for the app, select Authentication.
- In the Redirect URIs, add a redirect URL of type Web and valued
https://localhost:44321/signin-oidc
- In the Advanced settings section set Logout URL to
https://localhost:44321/signout-oidc
- In the Advanced settings | Implicit grant section, check ID tokens as this sample requires the Implicit grant flow to be enabled to sign in the user.
- Select Save.
- In the Redirect URIs, add a redirect URL of type Web and valued
Note that unless the Web App calls a Web API no certificate or secret is needed.
- replace the
Instance
value with the relevant authority value of the US Government cloud that ishttps://login.microsoftonline.us
- For other National Clouds go to List of authority of National Clouds
- replace the
ClientID
value with the Application ID from the application you registered in Application Registration portal on Step 2. - replace the
TenantId
value with the Tenant ID where you registered your Application on Step 2.
-
Run the following command to create a sample from the command line using the
SingleOrg
template:dotnet new mvc --auth SingleOrg --client-id <Enter_the_Application_Id_here> --tenant-id <yourTenantId>
Note: Replace
Enter_the_Application_Id_here
with the Application Id from the application Id you just registered in the Application Registration Portal and<yourTenantId>
with the Directory (tenant) ID where you created your application. -
Open the generated project (.csproj) in Visual Studio, and save the solution.
-
Add the
Microsoft.Identity.Web.csproj
project, which is located at the root of this sample repo, to your solution (Add Existing Project ...). It's used to simplify signing-in and, in the next tutorial phases, to get a token -
Add a reference from your newly generated project to
Microsoft.Identity.Web
(right click on the Dependencies node under your new project, and choose Add Reference ..., and then in the projects tab find theMicrosoft.Identity.Web
project) -
Open the Startup.cs file and:
- at the top of the file, add the following using directive:
using Microsoft.Identity.Web;
-
in the
ConfigureServices
method, replace the two following lines:services.AddAuthentication(AzureADDefaults.AuthenticationScheme) .AddAzureAD(options => Configuration.Bind("AzureAd", options));
by this line:
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.
- Change the
Properties\launchSettings.json
file to ensure that you start your web app from https://localhost:44321 as registered. For this:
- update the
sslPort
of theiisSettings
section to be44321
- in the
applicationUrl
property of usehttps://localhost:44321
-
Build the solution and run it.
-
Open your web browser and make a request to the app. Accept the IIS Express SSL certificate if needed. The app immediately attempts to authenticate you via the Microsoft identity platform endpoint. Sign in with your personal account or with work or school account.
ASP.NET core applications create session cookies that represent the identity of the caller. Some Safari users using iOS 12 had issues, which are described in ASP.NET Core #4467 and the Web kit bugs database Bug 188165 - iOS 12 Safari breaks ASP.NET Core 2.1 OIDC authentication.
If your web site needs to be accessed from users using iOS 12, you probably want to disable the SameSite protection, but also ensure that state changes are protected with CSRF anti-forgery mechanism. See the how to fix section of Microsoft Security Advisory: iOS12 breaks social, WSFed, and OIDC logins #4647
This sample shows how to use the OpenID Connect ASP.NET Core middleware to sign in users from a single Azure AD tenant. The middleware is initialized in the Startup.cs
file by passing it the Client ID of the app, and the URL of the Azure AD tenant where the app is registered. These values are read from the appsettings.json
file. The middleware takes care of:
- Downloading the Azure AD metadata, finding the signing keys, and finding the issuer name for the tenant.
- Processing OpenID Connect sign-in responses by validating the signature and issuer in an incoming JWT, extracting the user's claims, and putting the claims in
ClaimsPrincipal.Current
. - Integrating with the session cookie ASP.NET Core middleware to establish a session for the user.
You can trigger the middleware to send an OpenID Connect sign-in request by decorating a class or method with the [Authorize]
attribute or by issuing a challenge (see the AccountController.cs file, which is part of ASP.NET Core):
The middleware in this project is created as a part of the open-source ASP.NET Core Security project.
These steps are encapsulated in the Microsoft.Identity.Web project, and in particular in the StartupHelper.cs file
- Learn how to enable any organization or any Microsoft accounts to sign in
- Learn how to enable your Web App to call a Web API on behalf of the signed-in user
To understand more about app registration, see: