Skip to content

Commit a68d997

Browse files
authored
first pass removing .NET 3.1
1 parent 0865fa2 commit a68d997

File tree

1 file changed

+12
-72
lines changed

1 file changed

+12
-72
lines changed

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

Lines changed: 12 additions & 72 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

@@ -59,21 +59,17 @@ This section describes how to configure a bearer token.
5959

6060
### Config file
6161

62+
You need 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": {
@@ -87,15 +83,15 @@ This section describes how to configure a bearer token.
8783

8884
#### Case where you used a custom App ID URI for your 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
},
10096
// more lines
10197
}
@@ -161,62 +157,6 @@ app.MapControllers();
161157
app.Run();
162158
```
163159

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-
220160
## Token validation
221161

222162
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:

0 commit comments

Comments
 (0)