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: 4-WebApp-your-API/4-2-B2C/README.md
+41-28Lines changed: 41 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ description: "How to secure a Web API built with ASP.NET Core using the Azure AD
19
19
20
20
This sample demonstrates an ASP.NET Core Web App application calling an ASP.NET Core Web API that is secured using Azure AD B2C.
21
21
22
-
1. The client ASP.NET Core Web App application uses the Microsoft Authentication Library [MSAL.NET](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet) to sign-in a user and obtain a JWT access token from **Azure AD B2C**:
22
+
1. The client ASP.NET Core Web App application uses the Microsoft Authentication Library [Microsoft Authentication Library (MSAL) for .NET](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet) to sign-in a user and obtain a JWT access token from **Azure AD B2C**:
23
23
1. The [Access Token](https://docs.microsoft.com/azure/active-directory/develop/access-tokens) is used as a bearer token to authenticate the user when calling the ASP.NET Core Web API.
24
24
25
25
The client web application essentially takes the following steps to sign-in the user and obtain a bearer token for the Web API:
@@ -210,13 +210,18 @@ NOTE: Remember, the To-Do list is stored in memory in this `TodoListService` app
This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.
219
+
1. Update the `Configure` method to include **app.UseAuthentication();** before **app.UseMvc();**
220
+
221
+
```Csharp
222
+
app.UseAuthentication();
223
+
app.UseMvc();
224
+
```
220
225
221
226
1. Change the `Properties\launchSettings.json` file to ensure that you start your web app from <https://localhost:44321> as registered. For this:
222
227
- update the `sslPort` of the `iisSettings` section to be `44321`
@@ -237,21 +242,6 @@ NOTE: Remember, the To-Do list is stored in memory in this `TodoListService` app
237
242
1. Copy the contents of **TodoListClient\views\ToDo** folder to the views folder of your project.
238
243
1. Modify the `Views\Shared\_Layout.cshtml` to add a link to the ***ToDolist* controller. Check the `Views\Shared\_Layout.cshtml`in the sample for reference.
239
244
1. Add a section name **TodoList**in the appsettings.json file and add the keys `TodoListScope`, `TodoListBaseAddress`.
240
-
1. Update the `configureServices` method in`startup.cs` to add the MSAL library and a token cache.
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Configuration["TodoList:TodoListScope"] }, configSectionName: "AzureAdB2C");
246
-
services.AddInMemoryTokenCaches();
247
-
```
248
-
249
-
1. Update the `Configure` method to include **app.UseAuthentication();** before **app.UseMvc();**
250
-
251
-
```Csharp
252
-
app.UseAuthentication();
253
-
app.UseMvc();
254
-
```
255
245
256
246
### Creating the Web API project (TodoListService)
257
247
@@ -313,19 +303,42 @@ using Microsoft.Identity.Web.Client.TokenCacheProviders;
313
303
app.UseMvc();
314
304
```
315
305
316
-
`AddMicrosoftWebApi` does the following:
317
-
- add the **Jwt**BearerAuthenticationScheme (Note the replacement of **BearerAuthenticationScheme** by **Jwt**BearerAuthenticationScheme)
318
-
- set the authority to be the Microsoft identity platform identity
319
-
- sets the audiences to validate
320
-
- register an issuer validator that accepts issuers to be in the Microsoft identity platform clouds.
321
-
322
-
The implementations of these classes are in the `Microsoft.Identity.Web` library (and folder), and they are designed to be reusable in your applications (Web apps and Web apis). You are encouraged to browse the code in the library to understand the changes in detail.
323
-
324
306
### Create the TodoListController.cs file
325
307
326
308
1. Add a folder named `Models` and then create a new file named `TodoItem.cs`. Copy the contents of the TodoListClient\Models\TodoItem.cs in this file.
327
309
1. Create a new Controller named `TodoListController` and copy and paste the code from the sample (\TodoListService\Controllers\TodoListController.cs) to this controller.
328
310
311
+
## About the code
312
+
313
+
### Code for the Web App (TodoListClient)
314
+
315
+
In `Startup.cs`, below lines of code enables Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School Accounts.
1. AddMicrosoftIdentityWebAppAuthentication : This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.
324
+
1. EnableTokenAcquisitionToCallDownstreamApi : Enables the web app to call the protected API ToDoList Api.
325
+
1. AddInMemoryTokenCaches: Adds an in memory token cache provider, which will cache the Access Tokens acquired for the Web API.
326
+
327
+
### Code for the Web API (ToDoListService)
328
+
329
+
In `Startup.cs`, below lines of code protects the web API with Microsoft identity platform.
0 commit comments