You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+80-42Lines changed: 80 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,63 +2,89 @@
2
2
services: active-directory
3
3
platforms: dotnet
4
4
author: jmprieur
5
+
level: 200
6
+
service: ASP.NET Core Web App
7
+
endpoint: AAD V2
5
8
---
9
+
# Integrating Azure AD V2 into an ASP.NET Core web app
6
10
7
-
# Integrating Azure AD into an ASP.NET Core web app
8
-
9
-
This sample shows how to build a .NET MVC web app that uses OpenID Connect to sign-in users from a single Azure Active Directory (Azure AD) tenant using the ASP.NET Core OpenID Connect middleware.
11
+
This sample shows how to build a .NET Core MVC Web app that uses OpenID Connect to sign in users with their Work and School or Microsoft personal account (formerly live accounts). It leverages the ASP.NET Core OpenID Connect middleware.
10
12
11
13
For more information on how the protocols work in this scenario and other scenarios, see [Authentication Scenarios for Azure AD](http://go.microsoft.com/fwlink/?LinkId=394414).
12
14
13
15
## How to run this sample
14
16
15
-
If you are interested in ASP.NET Core 1.1, please look at branch [aspnet_core_1_1](https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect-aspnetcore/tree/aspnet_core_1_1).
16
-
17
17
To run this sample:
18
-
- Install .NET Core for Windows by following the instructions at [.NET and C# - Get Started in 10 Minutes](https://www.microsoft.com/net/core). In addition to developing on Windows, you can develop on [Linux](https://www.microsoft.com/net/core#linuxredhat), [Mac](https://www.microsoft.com/net/core#macos), or [Docker](https://www.microsoft.com/net/core#dockercmd).
18
+
19
+
- Install .NET Core (for example for Windows) by following the instructions at [.NET and C# - Get Started in 10 Minutes](https://www.microsoft.com/net/core). In addition to developing on Windows, you can develop on [Linux](https://www.microsoft.com/net/core#linuxredhat), [Mac](https://www.microsoft.com/net/core#macos), or [Docker](https://www.microsoft.com/net/core#dockercmd).
19
20
- An Azure AD tenant. For more information on how to obtain an Azure AD tenant, see [How to get an Azure AD tenant](https://azure.microsoft.com/documentation/articles/active-directory-howto-tenant/).
20
21
21
22
### Step 1: Register the sample with your Azure AD tenant
22
23
23
-
1. Sign in to the [Azure portal](https://portal.azure.com).
24
-
25
-
2. On the top bar, select your account. Under the **DIRECTORY** list, choose the Active Directory tenant where you wish to register your app. If there isn't a **DIRECTORY** list in the drop down menu, skip this step, as you only have a single tenant associated with your Azure account. For more information, see [How to get an Azure Active Directory tenant](https://docs.microsoft.com/azure/active-directory/develop/active-directory-howto-tenant).
26
-
27
-
3. In the left navigation sidebar, select **Azure Active Directory**. If you don't see **Azure Active Directory** in the list, select **More Services** and choose **Azure Active Directory** in the **SECURITY + IDENTITY** section of the service list.
28
-
29
-
4. From the sidebar, select **App registrations**.
30
-
31
-
5. Select **New application registration** and provide a friendly name for the app, app type, and sign-on URL:
6. On the **Properties** blade, set the **Logout URL** to `http://localhost:5000/signout-oidc` and select **Save**.
38
-
39
-
7. From the Azure portal, note the following information:
40
-
41
-
**The Tenant domain:** See the **App ID URI** base URL. For example: `contoso.onmicrosoft.com`
42
-
43
-
**The Tenant ID:** See the **Endpoints** blade. Record the GUID from any of the endpoint URLs. For example: `da41245a5-11b3-996c-00a8-4d99re19f292`
44
-
45
-
**The Application ID (Client ID):** See the **Properties** blade. For example: `ba74781c2-53c2-442a-97c2-3d60re42f403`
24
+
1. Sign in to the [Application registration portal](https://apps.dev.microsoft.com) either a personal or work or school Microsoft account
25
+
1. Look at your list of Microsoft apps
26
+
1. Click **Add an app**, and give it a name. Then press **Create**. The portal will assign your app a globally unique Application ID that you'll use later in your code.
27
+
1. Click on **Add Platform**, and in the dialog press the **Web** icon.
28
+
1. In the Redirect URLs field, add `http://localhost:5000/` and `http://localhost:5000/signin-oidc`
46
29
47
30
> [!NOTE]
48
31
> The base address in the **Sign-on URL** and **Logout URL** settings is `http://localhost:5000`. This localhost address allows the sample app to run insecurely from your local system. Port 5000 is the default port for the [Kestrel server](https://docs.microsoft.com/aspnet/core/fundamentals/servers/kestrel). Update these URLs if you configure the app for production use (for example, `https://www.contoso.com/signin-oidc` and `https://www.contoso.com/signout-oidc`).
49
32
50
33
### Step 2: Create the sample
51
34
52
-
This sample was created from the 2.0 [dotnet new mvc](https://docs.microsoft.com/dotnet/core/tools/dotnet-new?tabs=netcore2x) template with `SingleOrg` authentication. You can create the sample from the command line or clone/download this repository:
35
+
This sample was created from the dotnet core 2.0 template [dotnet new mvc](https://docs.microsoft.com/dotnet/core/tools/dotnet-new?tabs=netcore2x) template with `SingleOrg` authentication, and then tweaked to let it support tokens for the Azure AD V2 endpoint. You can create the sample from the command line or clone/download this repository:
36
+
37
+
#### To create the sample from the command line
53
38
54
-
-To create the sample from the command line, execute the following command:
dotnet new mvc --auth SingleOrg --client-id <CLIENT_ID_(APP_ID)>
58
43
```
59
-
Use the values that you recorded from the Azure portal for \<CLIENT\_ID\_(APP\_ID)>, \<TENANT\_ID>, and \<TENANT\_DOMAIN>.
60
44
61
-
- To clone/download this sample, execute the following command from your shell or command line:
45
+
Use the value that you recorded from the Azure portal for \<CLIENT\_ID\_(APP\_ID)>.
46
+
47
+
- Then:
48
+
49
+
- Modify the `Configure` method in `Extensions\AzureAdAuthenticationBuilderExtensions.cs` file. This method show be as follows (the changed lines are the lines containing `Authority`, and `ValidateIssuer`)
@@ -70,30 +96,42 @@ This sample was created from the 2.0 [dotnet new mvc](https://docs.microsoft.com
70
96
71
97
Build the solution and run it.
72
98
73
-
Make a request to the app. The app immediately attempts to authenticate you via Azure AD. Sign in with the username and password of a user account that is in your Azure AD tenant. You can also use your tenant's Global Administrator account. If you wish to create a user in the tenant, select **Add a user** from the **Quick tasks** panel. The **Quick tasks** panel is found on the Azure AD tenant's blade in the portal.
99
+
Make a request to the app. The app immediately attempts to authenticate you via Azure AD. Sign in with the username and password of a user account that is in your Azure AD tenant or your microsoft personal account.
74
100
75
101
## About The code
76
102
77
-
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, which is read from the `appsettings.json` file. The middleware takes care of:
103
+
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, which is read from the `appsettings.json` file. The middleware takes care of:
104
+
78
105
- Downloading the Azure AD metadata, finding the signing keys, and finding the issuer name for the tenant.
79
106
- 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`.
80
-
- Integrating with the session cookie ASP.NET Core middleware to establish a session for the user.
107
+
- Integrating with the session cookie ASP.NET Core middleware to establish a session for the user.
81
108
82
109
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):
The middleware in this project is created as a part of the open source [ASP.NET Security](https://github.com/aspnet/Security) project.
126
+
The specific Azure AD V2 code is in `Configure(string name, OpenIdConnectOptions options)`:
127
+
128
+
```CSharp
129
+
options.Authority=$"{_azureOptions.Instance}common/v2.0"; // V2 specific
130
+
options.TokenValidationParameters.ValidateIssuer=false; // accept any tenant
131
+
```
132
+
133
+
The first line tells the middleware to let sign-in users with the Azure AD V2 endpoint, that is with their work and school account or Microsoft personal account.
134
+
135
+
The second line tells the middleware to not validate the tenants. If you want to validate the tenants, you can set `ValidateIssuer` to true, and add a delegate as the `options.TokenValidationParameters.IssuerValidator` property.
136
+
137
+
The middleware in this project is created as a part of the open-source [ASP.NET Security](https://github.com/aspnet/Security) project.
0 commit comments