|
1 |
| -# Project Name |
| 1 | +--- |
| 2 | +services: active-directory |
| 3 | +platforms: dotnet |
| 4 | +author: jmprieur |
| 5 | +--- |
2 | 6 |
|
3 |
| -(short, 1-3 sentenced, description of the project) |
| 7 | +# Integrating Azure AD into an ASP.NET Core web app |
4 | 8 |
|
5 |
| -## Features |
| 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. |
6 | 10 |
|
7 |
| -This project framework provides the following features: |
| 11 | +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). |
8 | 12 |
|
9 |
| -* Feature 1 |
10 |
| -* Feature 2 |
11 |
| -* ... |
| 13 | +## How to run this sample |
12 | 14 |
|
13 |
| -## Getting Started |
| 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). |
14 | 16 |
|
15 |
| -### Prerequisites |
| 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). |
| 19 | +- 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/). |
16 | 20 |
|
17 |
| -(ideally very short, if any) |
| 21 | +### Step 1: Register the sample with your Azure AD tenant |
18 | 22 |
|
19 |
| -- OS |
20 |
| -- Library version |
21 |
| -- ... |
| 23 | +1. Sign in to the [Azure portal](https://portal.azure.com). |
22 | 24 |
|
23 |
| -### Installation |
| 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). |
24 | 26 |
|
25 |
| -(ideally very short) |
| 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. |
26 | 28 |
|
27 |
| -- npm install [package name] |
28 |
| -- mvn install |
29 |
| -- ... |
| 29 | +4. From the sidebar, select **App registrations**. |
30 | 30 |
|
31 |
| -### Quickstart |
32 |
| -(Add steps to get up and running quickly) |
| 31 | +5. Select **New application registration** and provide a friendly name for the app, app type, and sign-on URL: |
| 32 | + **Name**: **WebApp-OpenIDConnect-DotNet** |
| 33 | + **Application Type**: **Web app / API** |
| 34 | + **Sign-on URL**: `http://localhost:5000/signin-oidc` |
| 35 | + Select **Create** to register the app. |
33 | 36 |
|
34 |
| -1. git clone [repository clone url] |
35 |
| -2. cd [respository name] |
36 |
| -3. ... |
| 37 | +6. On the **Properties** blade, set the **Logout URL** to `http://localhost:5000/signout-oidc` and select **Save**. |
37 | 38 |
|
| 39 | +7. From the Azure portal, note the following information: |
38 | 40 |
|
39 |
| -## Demo |
| 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` |
40 | 46 |
|
41 |
| -A demo app is included to show how to use the project. |
| 47 | +> [!NOTE] |
| 48 | +> 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`). |
42 | 49 |
|
43 |
| -To run the demo, follow these steps: |
| 50 | +### Step 2: Create the sample |
44 | 51 |
|
45 |
| -(Add steps to start up the demo) |
| 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: |
46 | 53 |
|
47 |
| -1. |
48 |
| -2. |
49 |
| -3. |
| 54 | +- To create the sample from the command line, execute the following command: |
50 | 55 |
|
51 |
| -## Resources |
| 56 | + ```console |
| 57 | + dotnet new mvc --auth SingleOrg --client-id <CLIENT_ID_(APP_ID)> --tenant-id <TENANT_ID> --domain <TENANT_DOMAIN> |
| 58 | + ``` |
| 59 | + Use the values that you recorded from the Azure portal for \<CLIENT\_ID\_(APP\_ID)>, \<TENANT\_ID>, and \<TENANT\_DOMAIN>. |
52 | 60 |
|
53 |
| -(Any additional resources or related projects) |
| 61 | +- To clone/download this sample, execute the following command from your shell or command line: |
54 | 62 |
|
55 |
| -- Link to supporting information |
56 |
| -- Link to similar sample |
57 |
| -- ... |
| 63 | + ```console |
| 64 | + git clone https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect-aspnetcore.git |
| 65 | + ``` |
| 66 | + |
| 67 | + In the **appsettings.json* file, provide values for the `Domain`, `TenantId`, and `ClientID` that you recorded earlier from the Azure portal. |
| 68 | + |
| 69 | +### Step 3: Run the sample |
| 70 | + |
| 71 | +Build the solution and run it. |
| 72 | + |
| 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. |
| 74 | + |
| 75 | +## About The code |
| 76 | + |
| 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: |
| 78 | +- Downloading the Azure AD metadata, finding the signing keys, and finding the issuer name for the tenant. |
| 79 | +- 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. |
| 81 | + |
| 82 | +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): |
| 83 | + |
| 84 | +```csharp |
| 85 | +return Challenge( |
| 86 | + new AuthenticationProperties { RedirectUri = redirectUrl }, |
| 87 | + OpenIdConnectDefaults.AuthenticationScheme); |
| 88 | +``` |
| 89 | + |
| 90 | +Similarly, you can send a signout request: |
| 91 | + |
| 92 | +```csharp |
| 93 | +return SignOut( |
| 94 | + new AuthenticationProperties { RedirectUri = callbackUrl }, |
| 95 | + CookieAuthenticationDefaults.AuthenticationScheme, |
| 96 | + OpenIdConnectDefaults.AuthenticationScheme); |
| 97 | +``` |
| 98 | + |
| 99 | +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