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-web-app-call-api-acquire-token.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ ms.custom: aaddev
19
19
20
20
You've built your client application object. Now, you'll use it to acquire a token to call a web API. In ASP.NET or ASP.NET Core, calling a web API is done in the controller:
21
21
22
-
- Get a token for the web API by using the token cache. To get this token, you call the `AcquireTokenSilent` method.
22
+
- Get a token for the web API by using the token cache. To get this token, you call the MSAL `AcquireTokenSilent` method (or the equivalent in Microsoft.Identity.Web).
23
23
- Call the protected API, passing the access token to it as a parameter.
24
24
25
25
# [ASP.NET Core](#tab/aspnetcore)
@@ -51,7 +51,7 @@ public async Task<IActionResult> Profile()
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-web-app-call-api-call-api.md
+24-35Lines changed: 24 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,10 @@ ms.custom: aaddev
19
19
20
20
Now that you have a token, you can call a protected web API.
21
21
22
+
## Call a protected web API
23
+
24
+
Calling a protected web API depends on your language and framework of choice:
25
+
22
26
# [ASP.NET Core](#tab/aspnetcore)
23
27
24
28
Here's simplified code for the action of the `HomeController`. This code gets a token to call Microsoft Graph. Code has been added to show how to call Microsoft Graph as a REST API. The URL for the Microsoft Graph API is provided in the appsettings.json file and is read in a variable named `webOptions`:
@@ -37,41 +41,26 @@ Here's simplified code for the action of the `HomeController`. This code gets a
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-web-app-call-api-sign-in.md
+1-29Lines changed: 1 addition & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,35 +27,7 @@ To clear the token-cache entry associated with the account that signed out, your
27
27
28
28
# [ASP.NET Core](#tab/aspnetcore)
29
29
30
-
For ASP.NET Core, the interception mechanism is illustrated in the `AddMsal()` method of [WebAppServiceCollectionExtensions.cs#L151-L157](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/db7f74fd7e65bab9d21092ac1b98a00803e5ceb2/Microsoft.Identity.Web/WebAppServiceCollectionExtensions.cs#L151-L157).
31
-
32
-
The Logout URL that you previously registered for your application enables you to implement single sign-out. The Microsoft identity platform `logout` endpoint calls your Logout URL. This call happens if the sign-out started from your web app, or from another web app or the browser. For more information, see [Single sign-out](v2-protocols-oidc.md#single-sign-out).
The code for `RemoveAccountAsync` is available from [Microsoft.Identity.Web/TokenAcquisition.cs#L264-L288](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/db7f74fd7e65bab9d21092ac1b98a00803e5ceb2/Microsoft.Identity.Web/TokenAcquisition.cs#L264-L288).
30
+
Microsoft.Identity.Web takes care of implementing sign-out for you.
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-web-app-sign-user-app-configuration.md
+49-98Lines changed: 49 additions & 98 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,112 +203,63 @@ The initialization code is different depending on the platform. For ASP.NET Core
203
203
204
204
# [ASP.NET Core](#tab/aspnetcore)
205
205
206
-
In ASP.NET Core web apps (and web APIs), the application is protected because you have a `[Authorize]` attribute on the controllers or the controller actions. This attribute checks that the user is authenticated. The code that's initializing the application is in the Startup.cs file.
206
+
In ASP.NET Core web apps (and web APIs), the application is protected because you have a `[Authorize]` attribute on the controllers or the controller actions. This attribute checks that the user is authenticated. The code that's initializing the application is in the *Startup.cs* file.
207
207
208
208
To add authentication with the Microsoft identity platform (formerly Azure AD v2.0), you'll need to add the following code. The comments in the code should be self-explanatory.
209
209
210
210
> [!NOTE]
211
-
> If you start your project with the default ASP.NET Core web project within Visual studioor by using `dotnet new mvc`, the method `AddAzureAD`is available by default. That's because the related packages are automatically loaded.
212
-
>
213
-
>If you build a project from scratch and are trying to use the following code, we suggest that you add the NuGet package **Microsoft.AspNetCore.Authentication.AzureAD.UI** to your project to make the `AddAzureAD` method available.
211
+
> If you start your project with the default ASP.NET Core web project within Visual Studioor by using `dotnet new mvc--auth SingleAuth`or`dotnet new webapp --auth SingleAuth`, you'll see code like the following: `services.AddAuthentication(AzureADDefaults.AuthenticationScheme).AddAzureAD(options => Configuration.Bind("AzureAd", options));`.
212
+
>
213
+
>This code uses the legacy **Microsoft.AspNetCore.Authentication.AzureAD.UI** NuGet package which is used to create an Azure AD v1.0 application. This article explains how to create a Microsoft identity platform (Azure AD v2.0) application which replaces that code.
214
214
215
-
The following code is available from [Startup.cs#L33-L34](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/faa94fd49c2da46b22d6694c4f5c5895795af26d/1-WebApp-OIDC/1-1-MyOrg/Startup.cs#L33-L34).
215
+
1. Add the [Microsoft.Identity.Web](https://www.nuget.org/packages/Microsoft.Identity.Web) and [Microsoft.Identity.Web.UI](https://www.nuget.org/packages/Microsoft.Identity.Web.UI) NuGet packages to your project. Remove the Microsoft.AspNetCore.Authentication.AzureAD.UI NuGet package if it's present.
216
216
217
-
```csharp
218
-
public class Startup
219
-
{
220
-
...
217
+
2. Update the code in`ConfigureServices` so that it uses the `AddSignIn`and`AddMicrosoftIdentityUI` methods.
221
218
222
-
// This method is called by the runtime. Use this method to add services to the container.
223
-
public void ConfigureServices(IServiceCollection services)
224
-
{
219
+
```c#
220
+
public class Startup
221
+
{
225
222
...
226
-
// Sign in users with the Microsoft identity platform
The `AddMicrosoftIdentityPlatformAuthentication` extension method is defined in [Microsoft.Identity.Web/WebAppServiceCollectionExtensions.cs#L23](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/faa94fd49c2da46b22d6694c4f5c5895795af26d/Microsoft.Identity.Web/WebAppServiceCollectionExtensions.cs#L23). It:
241
-
242
-
- Adds the authentication service.
243
-
- Configures options to read the configuration file.
244
-
- Configures the OpenID Connect options so that the used authority is the Microsoft identity platform (formerly Azure AD v2.0) endpoint.
245
-
- Validates the issuer of the token.
246
-
- Ensures that the claims corresponding to name are mapped from the `preferred_username` claim in the ID token.
247
-
248
-
In addition to the configuration, you can specify the name of the configuration section when calling `AddMicrosoftIdentityPlatformAuthentication`. By default, it's `AzureAd`.
249
-
250
-
Tracing OpenId Connect middleware events can help you troubleshoot your web application if authentication doesn't work. Setting `subscribeToOpenIdConnectMiddlewareDiagnosticsEvents` to `true` will show you how information gets elaborated by the set of ASP.NET Core middleware as it progresses from the HTTP response to the identity of the user in `HttpContext.User`.
251
-
252
-
```csharp
253
-
///<summary>
254
-
/// Add authentication with the Microsoft identity platform.
255
-
/// This method expects the configuration file to have a section named "AzureAd"with the necessary settings to initialize authentication options.
256
-
///</summary>
257
-
///<param name="services">Service collection to which to add this authentication scheme</param>
The `AadIssuerValidator`class enables the issuer of the token to be validated in many cases. This class works with a v1.0 or v2.0 token, a single-tenant or multitenant application, or an application that signs in users with their personal Microsoft accounts in the Azure public cloud or national clouds. It's available from [Microsoft.Identity.Web/Resource/AadIssuerValidator.cs](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/master/Microsoft.Identity.Web/Resource/AadIssuerValidator.cs).
223
+
// This method gets called by the runtime. Use this method to add services to the container.
224
+
public void ConfigureServices(IServiceCollection services)
225
+
{
226
+
services.AddSignIn(Configuration, "AzureAd");
227
+
228
+
services.AddRazorPages().AddMvcOptions(options=>
229
+
{
230
+
var policy = new AuthorizationPolicyBuilder()
231
+
.RequireAuthenticatedUser()
232
+
.Build();
233
+
options.Filters.Add(new AuthorizeFilter(policy));
234
+
}).AddMicrosoftIdentityUI();
235
+
```
236
+
237
+
3. In the `Configure` method in*Startup.cs*, enable authentication with a call to `app.UseAuthentication();`
238
+
239
+
```c#
240
+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
241
+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
242
+
{
243
+
// more code here
244
+
app.UseAuthentication();
245
+
app.UseAuthorization();
246
+
// more code here
247
+
}
248
+
```
249
+
250
+
In the code above:
251
+
- The `AddSignIn` extension method is defined in**Microsoft.Identity.Web**. It:
252
+
- Adds the authentication service.
253
+
- Configures options to read the configuration file (here from the "AzureAD" section)
254
+
- Configures the OpenID Connect options so that the authority is the Microsoft identity platform endpoint.
255
+
- Validates the issuer of the token.
256
+
- Ensures that the claims corresponding to name are mapped from the `preferred_username` claim in the ID token.
257
+
258
+
- In addition to the configuration object, you can specify the name of the configuration section when calling `AddSignIn`. By default, it's `AzureAd`.
259
+
260
+
-`AddSignIn` has other parameters for advanced scenarios. For example, tracing OpenID Connect middleware events can help you troubleshoot your web application if authentication doesn't work. Setting the optional parameter `subscribeToOpenIdConnectMiddlewareDiagnosticsEvents` to `true` will show you how information is processed by the set of ASP.NET Core middleware as it progresses from the HTTP response to the identity of the user in `HttpContext.User`.
261
+
262
+
- The `AddMicrosoftIdentityUI` extension method is defined in**Microsoft.Identity.Web.UI**. It provides a default controller to handle sign-out.
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-web-app-sign-user-production.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,8 @@ Make sure you understand possible issues with new versions of the Chrome browser
39
39
> [!div class="nextstepaction"]
40
40
> [How to handle SameSite cookie changes in Chrome browser](howto-handle-samesite-cookie-changes-chrome-browser.md)
41
41
42
+
The Microsoft.Identity.Web NuGet package handles the most common SameSite issues.
43
+
42
44
### Scenario for calling web APIs
43
45
44
46
After your web app signs in users, it can call web APIs on behalf of the signed-in users. Calling web APIs from the web app is the object of the following scenario:
0 commit comments