Skip to content

Commit 21291f0

Browse files
authored
Merge pull request #221892 from MicrosoftDocs/main
12/19 AM Publish
2 parents 9dfd761 + df69d35 commit 21291f0

File tree

53 files changed

+1603
-1540
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1603
-1540
lines changed

articles/active-directory/develop/includes/web-app/quickstart-aspnet-core.md

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ ms.service: active-directory
99
ms.subservice: develop
1010
ms.topic: quickstart
1111
ms.workload: identity
12-
ms.date: 11/22/2021
12+
ms.date: 12/19/2022
1313
ms.author: jmprieur
1414
ms.custom: "devx-track-csharp, aaddev, identityplatformtop40, scenarios:getting-started, languages:aspnet-core"
1515
#Customer intent: As an application developer, I want to know how to write an ASP.NET Core web app that can sign in personal Microsoft accounts and work/school accounts from any Azure Active Directory instance, then access their data in Microsoft Graph on their behalf.
1616
---
1717

18-
In this quickstart, you download and run a code sample that demonstrates how an ASP.NET Core web app can sign in users from any Azure Active Directory (Azure AD) organization.
18+
The following quickstart uses a ASP.NET Core web app code sample to demonstrate how to sign in users from any Azure Active Directory (Azure AD) organization.
1919

2020
See [How the sample works](#how-the-sample-works) for an illustration.
2121

2222
## Prerequisites
2323

24-
* [Visual Studio](https://visualstudio.microsoft.com/vs/) or [Visual Studio Code](https://code.visualstudio.com/)
25-
* [.NET Core SDK 3.1+](https://dotnet.microsoft.com/download)
24+
* [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) or [Visual Studio Code](https://code.visualstudio.com/)
25+
* [.NET Core SDK 6.0+](https://dotnet.microsoft.com/download)
2626

2727
## Register and download your quickstart application
2828

29-
#### Step 1: Register your application
30-
1. Sign in to the <a href="https://portal.azure.com/" target="_blank">Azure portal</a>.
31-
1. If you have access to multiple tenants, use the **Directories + subscriptions** filter :::image type="icon" source="../../media/common/portal-directory-subscription-filter.png" border="false"::: in the top menu to switch to the tenant in which you want to register the application.
29+
### Step 1: Register your application
30+
1. Sign in to the [Azure portal](https://portal.azure.com/).
31+
1. If access to multiple tenants is available, use the **Directories + subscriptions** filter :::image type="icon" source="../../media/common/portal-directory-subscription-filter.png" border="false"::: in the top menu to switch to the tenant in which to register the application.
3232
1. Search for and select **Azure Active Directory**.
3333
1. Under **Manage**, select **App registrations** > **New registration**.
34-
1. For **Name**, enter a name for your application. For example, enter **AspNetCore-Quickstart**. Users of your app will see this name, and you can change it later.
34+
1. For **Name**, enter a name for the application. For example, enter **AspNetCore-Quickstart**. Users of the app will see this name, and can be changed later.
3535
1. Set the **Redirect URI** type to **Web** and value to `https://localhost:44321/signin-oidc`.
3636
1. Select **Register**.
3737
1. Under **Manage**, select **Authentication**.
@@ -43,47 +43,66 @@ See [How the sample works](#how-the-sample-works) for an illustration.
4343
1. Select **In 1 year** for the secret's expiration.
4444
1. Select **Add** and immediately record the secret's **Value** for use in a later step. The secret value is *never displayed again* and is irretrievable by any other means. Record it in a secure location as you would any password.
4545

46-
#### Step 2: Download the ASP.NET Core project
46+
### Step 2: Download the ASP.NET Core project
4747

4848
[Download the ASP.NET Core solution](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/archive/aspnetcore3-1-callsgraph.zip)
4949

50-
[!INCLUDE [active-directory-develop-path-length-tip](../../../../../includes/active-directory-develop-path-length-tip.md)]
50+
> [!Note]
51+
> The code sample currently targets ASP.NET Core 3.1. The sample can be updated to use .NET Core 6.0 and is covered in the following steps: [Update the sample code to ASP.NET Core 6.0](#step-4-update-the-sample-code-to-aspnet-core-60)
52+
This quickstart will be deprecated in the near future and will be updated to use .NET 6.0.
5153

52-
#### Step 3: Configure your ASP.NET Core project
53-
1. Extract the .zip archive into a local folder near the root of your drive. For example, extract into *C:\Azure-Samples*.
54+
### Step 3: Configure your ASP.NET Core project
5455

55-
We recommend extracting the archive into a directory near the root of your drive to avoid errors caused by path length limitations on Windows.
56-
1. Open the solution in Visual Studio 2019.
57-
1. Open the *appsettings.json* file and modify the following code:
56+
1. Extract the *.zip* file to a local folder that's close to the root of the disk to avoid errors caused by path length limitations on Windows. For example, extract to *C:\Azure-Samples*.
57+
1. Open the solution in the chosen code editor.
58+
1. In *appsettings.json*, replace the values of `ClientId`, and `TenantId`. The value for the application (client) ID and the directory (tenant) ID, can be found in the app's **Overview** page on the Azure portal.
5859

5960
```json
6061
"Domain": "[Enter the domain of your tenant, e.g. contoso.onmicrosoft.com]",
6162
"ClientId": "Enter_the_Application_Id_here",
6263
"TenantId": "common",
6364
```
6465

65-
- Replace `Enter_the_Application_Id_here` with the application (client) ID of the application that you registered in the Azure portal. You can find the **Application (client) ID** value on the app's **Overview** page.
66-
- Replace `common` with one of the following:
67-
- If your application supports **Accounts in this organizational directory only**, replace this value with the directory (tenant) ID (a GUID) or the tenant name (for example, `contoso.onmicrosoft.com`). You can find the **Directory (tenant) ID** value on the app's **Overview** page.
68-
- If your application supports **Accounts in any organizational directory**, replace this value with `organizations`.
69-
- If your application supports **All Microsoft account users**, leave this value as `common`.
70-
- Replace `Enter_the_Client_Secret_Here` with the **Client secret** you created and recorded in an earlier step.
66+
- `Enter_the_Application_Id_Here` is the application (client) ID for the registered application.
67+
- Replace `Enter_the_Tenant_Info_Here` with one of the following:
68+
- If the application supports **Accounts in this organizational directory only**, replace this value with the directory (tenant) ID (a GUID) or tenant name (for example, `contoso.onmicrosoft.com`). The directory (tenant) ID can be found on the app's **Overview** page.
69+
- If the application supports **Accounts in any organizational directory**, replace this value with `organizations`.
70+
- If the application supports **All Microsoft account users**, leave this value as `common`.
71+
- Replace `Enter_the_Client_Secret_Here` with the **Client secret** that was created and recorded in an earlier step.
7172

7273
For this quickstart, don't change any other values in the *appsettings.json* file.
7374

74-
#### Step 4: Build and run the application
75+
### Step 4: Update the sample code to ASP.NET Core 6.0
7576

76-
Build and run the app in Visual Studio by selecting the **Debug** menu > **Start Debugging**, or by pressing the F5 key.
77+
To update this code sample to target ASP.NET Core 6.0, follow these steps:
7778

78-
You're prompted for your credentials, and then asked to consent to the permissions that your app requires. Select **Accept** on the consent prompt.
79+
1. Open WebApp-OpenIDConnect-DotNet.csproj
80+
1. Remove the following line:
81+
82+
```xml
83+
<TargetFramework>netcoreapp3.1</TargetFramework>
84+
```
85+
86+
1. Add the following line in its place:
87+
88+
```xml
89+
<TargetFramework>netcoreapp6.0</TargetFramework>
90+
```
91+
92+
This step will ensure that the sample is targeting the .NET Core 6.0 framework.
93+
94+
### Step 5: Build and run the application
95+
96+
Build and run the app in Visual Studio by selecting the **Debug** menu > **Start Debugging**, or by pressing the F5 key.
97+
98+
A prompt for credentials will appear, and then a request for consent to the permissions that the app requires. Select **Accept** on the consent prompt.
7999

80100
:::image type="content" source="../../media/quickstart-v2-aspnet-core-webapp/webapp-01-consent.png" alt-text="Screenshot of the consent dialog box, showing the permissions that the app is requesting from the user.":::
81101

82-
After consenting to the requested permissions, the app displays that you've successfully logged in using your Azure Active Directory credentials, and you'll see your email address in the "Api result" section of the page. This was extracted using Microsoft Graph.
102+
After consenting to the requested permissions, the app displays that sign-in has been successful using correct Azure Active Directory credentials. The user's account email address will be displayed in the *API result* section of the page. This was extracted using the Microsoft Graph API.
83103

84104
:::image type="content" source="../../media/quickstart-v2-aspnet-core-webapp-calls-graph/webapp-02-signed-in.png" alt-text="Web browser displaying the running web app and the user signed in":::
85105

86-
87106
## More information
88107

89108
This section gives an overview of the code required to sign in users and call the Microsoft Graph API on their behalf. This overview can be useful to understand how the code works, main arguments, and also if you want to add sign-in to an existing ASP.NET Core application and call Microsoft Graph. It uses [Microsoft.Identity.Web](../../microsoft-identity-web.md), which is a wrapper around [MSAL.NET](../../msal-overview.md).
@@ -130,15 +149,15 @@ The *Microsoft.AspNetCore.Authentication* middleware uses a `Startup` class that
130149

131150
The `AddAuthentication()` method configures the service to add cookie-based authentication. This authentication is used in browser scenarios and to set the challenge to OpenID Connect.
132151

133-
The line that contains `.AddMicrosoftIdentityWebApp` adds Microsoft identity platform authentication to your application. The application is then configured to sign in users based on the following information in the `AzureAD` section of the *appsettings.json* configuration file:
152+
The line that contains `.AddMicrosoftIdentityWebApp` adds Microsoft identity platform authentication to the application. The application is then configured to sign in users based on the following information in the `AzureAD` section of the *appsettings.json* configuration file:
134153

135154
| *appsettings.json* key | Description |
136155
|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
137156
| `ClientId` | Application (client) ID of the application registered in the Azure portal. |
138157
| `Instance` | Security token service (STS) endpoint for the user to authenticate. This value is typically `https://login.microsoftonline.com/`, indicating the Azure public cloud. |
139158
| `TenantId` | Name of your tenant or the tenant ID (a GUID), or `common` to sign in users with work or school accounts or Microsoft personal accounts. |
140159

141-
The `EnableTokenAcquisitionToCallDownstreamApi` method enables your application to acquire a token to call protected web APIs. `AddMicrosoftGraph` enables your controllers or Razor pages to benefit directly the `GraphServiceClient` (by dependency injection) and the `AddInMemoryTokenCaches` methods enables your app to benefit from a token cache.
160+
The `EnableTokenAcquisitionToCallDownstreamApi` method enables the application to acquire a token to call protected web APIs. `AddMicrosoftGraph` enables the controllers or Razor pages to benefit directly the `GraphServiceClient` (by dependency injection) and the `AddInMemoryTokenCaches` methods enables your app to benefit from a token cache.
142161

143162
The `Configure()` method contains two important methods, `app.UseAuthentication()` and `app.UseAuthorization()`, that enable their named functionality. Also in the `Configure()` method, you must register Microsoft Identity Web routes with at least one call to `endpoints.MapControllerRoute()` or a call to `endpoints.MapControllers()`:
144163

@@ -157,7 +176,7 @@ app.UseEndpoints(endpoints =>
157176

158177
### Protect a controller or a controller's method
159178

160-
You can protect a controller or its methods by applying the `[Authorize]` attribute to the controller's class or one or more of its methods. This `[Authorize]` attribute restricts access by allowing only authenticated users. If the user isn't already authenticated, an authentication challenge can be started to access the controller. In this quickstart, the scopes are read from the configuration file:
179+
The controller or its methods can be protected by applying the `[Authorize]` attribute to the controller's class or one or more of its methods. This `[Authorize]` attribute restricts access by allowing only authenticated users. If the user isn't already authenticated, an authentication challenge can be started to access the controller. In this quickstart, the scopes are read from the configuration file:
161180

162181
```csharp
163182
[AuthorizeForScopes(ScopeKeySection = "DownstreamApi:Scopes")]
@@ -174,7 +193,7 @@ public async Task<IActionResult> Index()
174193

175194
## Next steps
176195

177-
The GitHub repo that contains the ASP.NET Core code sample referenced in this quickstart includes instructions and more code samples that show you how to:
196+
The following GitHub repository contains the ASP.NET Core code sample referenced in this quickstart and more samples that show how to achieve the following:
178197

179198
- Add authentication to a new ASP.NET Core web application.
180199
- Call Microsoft Graph, other Microsoft APIs, or your own web APIs.

0 commit comments

Comments
 (0)