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: articles/active-directory/develop/scenario-protected-web-api-app-configuration.md
+17-78Lines changed: 17 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ manager: CelesteDG
8
8
ms.service: active-directory
9
9
ms.subservice: develop
10
10
ms.topic: conceptual
11
-
ms.date: 05/12/2022
11
+
ms.date: 12/09/2022
12
12
ms.author: jmprieur
13
13
#Customer intent: As an application developer, I want to know how to write a protected web API using the Microsoft identity platform for developers.
14
14
---
@@ -23,7 +23,7 @@ To configure the code for your protected web API, understand:
23
23
24
24
## What defines ASP.NET and ASP.NET Core APIs as protected?
25
25
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.
> 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.
53
53
>
54
54
> 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.
55
55
@@ -59,21 +59,17 @@ This section describes how to configure a bearer token.
59
59
60
60
### Config file
61
61
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
@@ -85,19 +81,18 @@ This section describes how to configure a bearer token.
85
81
}
86
82
```
87
83
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
89
85
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://`.
@@ -109,7 +104,7 @@ When an app is called on a controller action that holds an **[Authorize]** attri
109
104
110
105
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.
111
106
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.
113
108
114
109
#### ASP.NET for .NET 6.0
115
110
@@ -124,7 +119,7 @@ dotnet new webapi --auth SingleOrg
124
119
125
120
**Visual Studio** - To create a web API project in Visual Studio, select **File** > **New** > **Project** > **ASP.NET Core Web API**.
126
121
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.
128
123
129
124
```csharp
130
125
usingMicrosoft.AspNetCore.Authentication;
@@ -161,62 +156,6 @@ app.MapControllers();
161
156
app.Run();
162
157
```
163
158
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.
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*:
>IfyouuseMicrosoft.Identity.Webanddon'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
-
220
159
## Token validation
221
160
222
161
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:
The validators are associated with properties of the *TokenValidationParameters* class. The properties are initialized from the ASP.NET and ASP.NET Core configuration.
250
189
251
190
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.
0 commit comments