Skip to content

Commit d154c3b

Browse files
authored
Merge pull request #221005 from cilwerner/content-health-1
[GTD][Core Content] Remove .NET 3.1 references from scenario-protected-web-api-app-configuration.md
2 parents d57e1b8 + fea06c4 commit d154c3b

File tree

1 file changed

+17
-78
lines changed

1 file changed

+17
-78
lines changed

articles/active-directory/develop/scenario-protected-web-api-app-configuration.md

Lines changed: 17 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: CelesteDG
88
ms.service: active-directory
99
ms.subservice: develop
1010
ms.topic: conceptual
11-
ms.date: 05/12/2022
11+
ms.date: 12/09/2022
1212
ms.author: jmprieur
1313
#Customer intent: As an application developer, I want to know how to write a protected web API using the Microsoft identity platform for developers.
1414
---
@@ -23,7 +23,7 @@ To configure the code for your protected web API, understand:
2323

2424
## What defines ASP.NET and ASP.NET Core APIs as protected?
2525

26-
Like web apps, the ASP.NET and ASP.NET Core web APIs are protected because their controller actions are prefixed with the **[Authorize]** attribute. The controller actions can be called only if the API is called with an authorized identity.
26+
Like web apps, ASP.NET and ASP.NET Core web APIs are protected because their controller actions are prefixed with the **[Authorize]** attribute. The controller actions can be called only if the API is called with an authorized identity.
2727

2828
Consider the following questions:
2929

@@ -49,7 +49,7 @@ HttpResponseMessage response = await _httpClient.GetAsync(apiUri);
4949
```
5050

5151
> [!IMPORTANT]
52-
> A client application requests the bearer token to the Microsoft identity platform *for the web API*. The web API is the only application that should verify the token and view the claims it contains. Client apps should never try to inspect the claims in tokens.
52+
> A client application requests the bearer token to the Microsoft identity platform *for the web API*. The API is the only application that should verify the token and view the claims it contains. Client apps should never try to inspect the claims in tokens.
5353
>
5454
> In the future, the web API might require that the token be encrypted. This requirement would prevent access for client apps that can view access tokens.
5555
@@ -59,21 +59,17 @@ This section describes how to configure a bearer token.
5959

6060
### Config file
6161

62+
You need to specify the `TenantId` only if you want to accept access tokens from a single tenant (line-of-business app). Otherwise, it can be left as `common`. The different values can be:
63+
- A GUID (Tenant ID = Directory ID)
64+
- `common` can be any organization and personal accounts
65+
- `organizations` can be any organization
66+
- `consumers` are Microsoft personal accounts
67+
6268
```Json
6369
{
6470
"AzureAd": {
6571
"Instance": "https://login.microsoftonline.com/",
66-
"ClientId": "[Client_id-of-web-api-eg-2ec40e65-ba09-4853-bcde-bcb60029e596]",
67-
/*
68-
You need specify the TenantId only if you want to accept access tokens from a single tenant
69-
(line-of-business app).
70-
Otherwise, you can leave them set to common.
71-
This can be:
72-
- A GUID (Tenant ID = Directory ID)
73-
- 'common' (any organization and personal accounts)
74-
- 'organizations' (any organization)
75-
- 'consumers' (Microsoft personal accounts)
76-
*/
72+
"ClientId": "Enter_the_Application_(client)_ID_here"
7773
"TenantId": "common"
7874
},
7975
"Logging": {
@@ -85,19 +81,18 @@ This section describes how to configure a bearer token.
8581
}
8682
```
8783

88-
#### Case where you used a custom App ID URI for your web API
84+
#### Using a custom App ID URI for a web API
8985

90-
If you've accepted the default App ID URI proposed by the Azure portal, you don't need to specify the audience (see [Application ID URI and scopes](scenario-protected-web-api-app-registration.md#scopes-and-the-application-id-uri)). Otherwise, add an `Audience` property whose value is the App ID URI for your web API.
86+
If you've accepted the default App ID URI proposed by the Azure portal, you don't need to specify the audience (see [Application ID URI and scopes](scenario-protected-web-api-app-registration.md#scopes-and-the-application-id-uri)). Otherwise, add an `Audience` property whose value is the App ID URI for your web API. This typically starts with `api://`.
9187

9288
```Json
9389
{
9490
"AzureAd": {
9591
"Instance": "https://login.microsoftonline.com/",
96-
"ClientId": "[Client_id-of-web-api-eg-2ec40e65-ba09-4853-bcde-bcb60029e596]",
92+
"ClientId": "Enter_the_Application_(client)_ID_here",
9793
"TenantId": "common",
98-
"Audience": "custom App ID URI for your web API"
94+
"Audience": "Enter_the_Application_ID_URI_here"
9995
},
100-
// more lines
10196
}
10297
```
10398

@@ -109,7 +104,7 @@ When an app is called on a controller action that holds an **[Authorize]** attri
109104

110105
Microsoft recommends you use the [Microsoft.Identity.Web](https://www.nuget.org/packages/Microsoft.Identity.Web) NuGet package when developing a web API with ASP.NET Core.
111106

112-
_Microsoft.Identity.Web_ provides the glue between ASP.NET Core, the authentication middleware, and the [Microsoft Authentication Library (MSAL)](msal-overview.md) for .NET. It allows for a clearer, more robust developer experience and leverages the power of the Microsoft identity platform and Azure AD B2C.
107+
*Microsoft.Identity.Web* provides the glue between ASP.NET Core, the authentication middleware, and the [Microsoft Authentication Library (MSAL)](msal-overview.md) for .NET. It allows for a clearer, more robust developer experience and leverages the power of the Microsoft identity platform and Azure AD B2C.
113108

114109
#### ASP.NET for .NET 6.0
115110

@@ -124,7 +119,7 @@ dotnet new webapi --auth SingleOrg
124119

125120
**Visual Studio** - To create a web API project in Visual Studio, select **File** > **New** > **Project** > **ASP.NET Core Web API**.
126121

127-
Both the .NET CLI and Visual Studio project templates create a _Program.cs_ file that looks similar this code snippet. Notice the `Microsoft.Identity.Web` using directive and the lines containing authentication and authorization.
122+
Both the .NET CLI and Visual Studio project templates create a *Program.cs* file that looks similar to this code snippet. Notice `Microsoft.Identity.Web` using directive and the lines containing authentication and authorization.
128123

129124
```csharp
130125
using Microsoft.AspNetCore.Authentication;
@@ -161,62 +156,6 @@ app.MapControllers();
161156
app.Run();
162157
```
163158

164-
#### ASP.NET Core 3.1
165-
166-
167-
To create a new web API project by using the Microsoft.Identity.Web-enabled project templates in ASP.NET Core 3.1, see [Microsoft.Identity.Web - Web API project template](https://aka.ms/ms-id-web/webapi-project-templates).
168-
169-
To add Microsoft.Identity.Web to an existing ASP.NET Core 3.1 web API project, add this using directive to your _Program.cs_ file:
170-
171-
ASP.NET Core 3.1 uses the Microsoft.AspNetCore.Authentication.JwtBearer library. The middleware is initialized in the Startup.cs file.
172-
173-
```csharp
174-
using Microsoft.AspNetCore.Authentication.JwtBearer;
175-
```
176-
177-
The middleware is added to the web API by this instruction:
178-
179-
```csharp
180-
// This method gets called by the runtime. Use this method to add services to the container.
181-
public void ConfigureServices(IServiceCollection services)
182-
{
183-
services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
184-
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
185-
}
186-
```
187-
188-
Currently, the ASP.NET Core templates create Azure Active Directory (Azure AD) web APIs that sign in users within your organization or any organization. They don't sign in users with personal accounts. However, you can change the templates to use the Microsoft identity platform by using [Microsoft.Identity.Web](https://www.nuget.org/packages/Microsoft.Identity.Web) replacing the code in *Startup.cs*:
189-
190-
```csharp
191-
using Microsoft.Identity.Web;
192-
```
193-
194-
```csharp
195-
public void ConfigureServices(IServiceCollection services)
196-
{
197-
// Adds Microsoft Identity platform (AAD v2.0) support to protect this API
198-
services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
199-
.AddMicrosoftIdentityWebApi(Configuration, "AzureAd");
200-
201-
services.AddControllers();
202-
}
203-
```
204-
205-
Make sure you have `app.UseAuthentication()` and `app.UseAuthorization()` in the `Configure` method.
206-
207-
```csharp
208-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
209-
{
210-
// More code here
211-
app.UseAuthentication();
212-
app.UseAuthorization();
213-
214-
// More code here
215-
```
216-
217-
> [!NOTE]
218-
> If you use Microsoft.Identity.Web and don't set the `Audience` in *appsettings.json*, `$"{ClientId}"` is automatically used if you have set the [access token accepted version](scenario-protected-web-api-app-registration.md#accepted-token-version) to `2`, or for Azure AD B2C web APIs.
219-
220159
## Token validation
221160

222161
In the preceding snippet, the JwtBearer middleware, like the OpenID Connect middleware in web apps, validates the token based on the value of `TokenValidationParameters`. The token is decrypted as needed, the claims are extracted, and the signature is verified. The middleware then validates the token by checking for this data:
@@ -246,7 +185,7 @@ This table describes the validators:
246185

247186
#### Customizing token validation
248187

249-
The validators are associated with properties of the **TokenValidationParameters** class. The properties are initialized from the ASP.NET and ASP.NET Core configuration.
188+
The validators are associated with properties of the *TokenValidationParameters* class. The properties are initialized from the ASP.NET and ASP.NET Core configuration.
250189

251190
In most cases, you don't need to change the parameters. Apps that aren't single tenants are exceptions. These web apps accept users from any organization or from personal Microsoft accounts. Issuers in this case must be validated. Microsoft.Identity.Web takes care of the issuer validation as well.
252191

0 commit comments

Comments
 (0)