Skip to content

Commit 23327fb

Browse files
committed
Updated About the Code
1 parent 0872410 commit 23327fb

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

4-WebApp-your-API/4-2-B2C/README.md

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ description: "How to secure a Web API built with ASP.NET Core using the Azure AD
1919

2020
This sample demonstrates an ASP.NET Core Web App application calling an ASP.NET Core Web API that is secured using Azure AD B2C.
2121

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**:
2323
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.
2424

2525
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
210210
by this line:
211211
212212
```CSharp
213-
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
214-
.AddMicrosoftWebApp(Configuration, "AzureAdB2C")
215-
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Configuration["TodoList:TodoListScope"] }, configSectionName: "AzureAdB2C");
213+
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C")
214+
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { Configuration["TodoList:TodoListScope"] })
215+
.AddInMemoryTokenCaches();
216216
services.AddInMemoryTokenCaches();
217217
```
218218
219-
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+
```
220225
221226
1. Change the `Properties\launchSettings.json` file to ensure that you start your web app from <https://localhost:44321> as registered. For this:
222227
- 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
237242
1. Copy the contents of **TodoListClient\views\ToDo** folder to the views folder of your project.
238243
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.
239244
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.
241-
242-
```CSharp
243-
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
244-
.AddMicrosoftWebApp(Configuration, "AzureAdB2C")
245-
.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-
```
255245

256246
### Creating the Web API project (TodoListService)
257247

@@ -313,19 +303,42 @@ using Microsoft.Identity.Web.Client.TokenCacheProviders;
313303
app.UseMvc();
314304
```
315305

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-
324306
### Create the TodoListController.cs file
325307

326308
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.
327309
1. Create a new Controller named `TodoListController` and copy and paste the code from the sample (\TodoListService\Controllers\TodoListController.cs) to this controller.
328310

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.
316+
317+
```csharp
318+
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C")
319+
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { Configuration["TodoList:TodoListScope"] })
320+
.AddInMemoryTokenCaches();
321+
```
322+
323+
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.
330+
331+
```Csharp
332+
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
333+
.AddMicrosoftIdentityWebApi(options =>
334+
{
335+
Configuration.Bind("AzureAdB2C", options);
336+
337+
options.TokenValidationParameters.NameClaimType = "name";
338+
},
339+
options => { Configuration.Bind("AzureAdB2C", options); });
340+
```
341+
329342
### Deployment to Azure App Services
330343
331344
There are two web projects in this sample. To deploy them to **Azure App Services**, you'll need, for each one, to:

0 commit comments

Comments
 (0)