Skip to content

Commit 0872410

Browse files
committed
Updated Readme as per Review Comments
1 parent ba50129 commit 0872410

File tree

1 file changed

+71
-53
lines changed

1 file changed

+71
-53
lines changed

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

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@ description: "How to secure a Web API built with ASP.NET Core using the Azure AD
1414
[![Build status](https://identitydivision.visualstudio.com/IDDP/_apis/build/status/AAD%20Samples/.NET%20client%20samples/ASP.NET%20Core%20Web%20App%20tutorial)](https://identitydivision.visualstudio.com/IDDP/_build/latest?definitionId=819)
1515

1616
> The sample in this folder is part of a multi-chapter tutorial. The first phase is available at [An ASP.NET Core Web app signing-in users with the Microsoft identity platform in your organization](../1-WebApp-OIDC/1-1-MyOrg).
17-
>
18-
> This article (README.md) contains the full instructions on how to configure the sample. If you have gone through the [first chapter](../1-WebApp-OIDC/1-1-MyOrg) and already configured the client web application to sigh-in users, read through the [README-incremental-instructions.md](README-incremental-instructions.md) instead.
1917
2018
## Overview
2119

22-
This sample demonstrates a ASP.NET Core Web App application calling a ASP.NET Core Web API that is secured using Azure AD B2C.
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.
2321

24-
1. The client ASP.NET Core Web App application uses the Microsoft Authentication Library (MSAL) to obtain a JWT access token from **Azure AD B2C**:
25-
1. The access token is used as a bearer token to authenticate the user when calling the ASP.NET Core Web API.
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**:
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.
2624

2725
The client web application essentially takes the following steps to sign-in the user and obtain a bearer token for the Web API:
2826

29-
1. Signs-in the user with local or social identities. When the user signs-in for the first time , a consent screen is presented. This consent screen lets the user consent for the application to access the web API( TodoListService).
27+
1. Signs-in the user with local or social identities.
3028
1. Acquires an access token for the Web API.
31-
1. Calls the Web API using the access token as a bearer token in the authentication header of the Http request. The Web API validates the caller using the ASP.NET JWT Bearer Authentication middleware.
29+
1. Calls the Web API using the access token as a bearer token in the authentication header of the Http request. The Web API authorizes the caller (user) using the ASP.NET JWT Bearer Authorization middleware.
3230

3331
![Topology](./ReadmeFiles/topology.png)
3432

@@ -40,7 +38,6 @@ This sample has a web API and a client web app, both built using the asp.net cor
4038

4139
- [Visual Studio](https://visualstudio.microsoft.com/downloads/)
4240
- An **Azure AD B2C** tenant. For more information see: [How to get an Azure AD B2C tenant](https://docs.microsoft.com/azure/active-directory-b2c/tutorial-create-tenant)
43-
- A user account in your **Azure AD B2C**. This sample will not work with a **personal Microsoft account**. Therefore, if you signed in to the [Azure portal](https://portal.azure.com) with a personal account and have never created a user account in your directory before, you need to do that now.
4441

4542
## Setup
4643

@@ -79,7 +76,7 @@ Please refer to: [Tutorial: Add identity providers to your applications in Azure
7976

8077
### Register the service app (TodoListService-aspnetcore-webapi)
8178

82-
Now you need to [register your web app in your B2C tenant](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-app-registration#register-a-web-application), so that it has its own Application ID.
79+
You need to [register your web app in your B2C tenant](https://docs.microsoft.com/azure/active-directory-b2c/active-directory-b2c-app-registration#register-a-web-application), so that it has its own Application ID.
8380

8481
#### Configure the service app (TodoListService-aspnetcore-webapi) to use your app registration
8582

@@ -114,17 +111,44 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
114111

115112
## Running the sample
116113

117-
> For Visual Studio Users
118-
>
119-
> Clean the solution, rebuild the solution, and run it. You might want to go into the solution properties and set both projects as startup projects, with the service project starting first.
114+
You can run the sample by using either Visual Studio or command line interface as shown below:
115+
116+
### Run the sample using Visual Studio
117+
118+
Clean the solution, rebuild the solution, and run it. You might want to go into the solution properties and set both projects as startup projects, with the service project starting first.
119+
120+
### Run the sample using a command line interface such as VS Code integrated terminal
121+
122+
#### Step 1. Install .NET Core dependencies
120123

121124
```console
122-
cd TodoListService-aspnetcore-webapi
123-
dotnet run
125+
cd TodoListService
126+
dotnet restore
124127
```
125128

129+
Then:
130+
In a separate console window, execute the following commands
131+
132+
```console
133+
cd ../
134+
cd Client
135+
dotnet restore
136+
```
137+
138+
#### Step 2. Trust development certificates
139+
140+
```console
141+
dotnet dev-certs https --clean
142+
dotnet dev-certs https --trust
143+
```
144+
145+
Learn more about [HTTPS in .NET Core](https://docs.microsoft.com/aspnet/core/security/enforcing-ssl).
146+
147+
#### Step 3. Run the applications
148+
149+
In both the console windows execute the below command:
150+
126151
```console
127-
cd TodoListClient-aspnetcore-webapi
128152
dotnet run
129153
```
130154

@@ -138,9 +162,9 @@ When you start the Web API from Visual Studio, depending on the browser you use,
138162
- an empty web page (case with Microsoft Edge)
139163
- or an error HTTP 401 (case with Chrome)
140164

141-
This behavior is expected as you are not authenticated. The client application will be authenticated, so it will be able to access the Web API.
165+
This behavior is expected as the browser is not authenticated. The client application will be authenticated, so it will be authorized to access the Web API.
142166

143-
Explore the sample by signing in into the TodoList client, adding items to the To Do list. If you stop the application without signing out, the next time you run the application, you won't be prompted to sign in again.
167+
Explore the sample by signing in into the TodoList client, adding items to the To-Do list. If you stop the application without signing out, the next time you run the application, you won't be prompted to sign in again.
144168

145169
NOTE: Remember, the To-Do list is stored in memory in this `TodoListService` app. Each time you run the projects, your To-Do list will get emptied.
146170

@@ -194,18 +218,16 @@ NOTE: Remember, the To-Do list is stored in memory in this `TodoListService` app
194218
195219
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.
196220
197-
1. Change the `Properties\launchSettings.json` file to ensure that you start your web app from <https://localhost:44321> as registered. For this:
221+
1. Change the `Properties\launchSettings.json` file to ensure that you start your web app from <https://localhost:44321> as registered. For this:
198222
- update the `sslPort` of the `iisSettings` section to be `44321`
199223
- in the `applicationUrl` property of use `https://localhost:44321`
200-
201-
- Then add the following code to inject the ToDoList service implementation in the client
224+
- Then add the following code to inject the ToDoList service implementation in the client
202225
203226
```CSharp
204-
// Add APIs
205227
services.AddTodoListService(Configuration);
206228
```
207229
208-
1. Open the `appsettings.json` file and copy the keys from the sample's corresponding file under the `AzureAd` and `TodoList` sections.
230+
1. Open the `appsettings.json` file and copy the keys from the sample's corresponding file under the `AzureAd` and `TodoList` sections.
209231

210232
#### Add a model (TodoListItem) and add the controller and views
211233

@@ -223,24 +245,27 @@ NOTE: Remember, the To-Do list is stored in memory in this `TodoListService` app
223245
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Configuration["TodoList:TodoListScope"] }, configSectionName: "AzureAdB2C");
224246
services.AddInMemoryTokenCaches();
225247
```
248+
226249
1. Update the `Configure` method to include **app.UseAuthentication();** before **app.UseMvc();**
227250
228-
```Csharp
229-
app.UseAuthentication();
230-
app.UseMvc();
231-
```
251+
```Csharp
252+
app.UseAuthentication();
253+
app.UseMvc();
254+
```
232255
233256
### Creating the Web API project (TodoListService)
234257
235258
The code for the TodoListService was created in the following way:
236259
237260
#### Step 1: Create the web api using the ASP.NET Core templates
238261
239-
```Text
240-
md TodoListService
241-
cd TodoListService
242-
dotnet new webapi -au=SingleOrg
243-
```
262+
1. Run the following command to create a sample from the command line using the `SingleOrg` template:
263+
264+
```Text
265+
md TodoListService
266+
cd TodoListService
267+
dotnet new webapi -au=SingleOrg
268+
```
244269
245270
1. Open the generated project (.csproj) in Visual Studio, and save the solution.
246271
@@ -280,6 +305,7 @@ using Microsoft.Identity.Web.Client.TokenCacheProviders;
280305
},
281306
options => { Configuration.Bind("AzureAdB2C", options); });
282307
```
308+
283309
- Add the method **app.UseAuthentication()** before **app.UseMvc()** in the `Configure` method
284310
285311
```Csharp
@@ -333,9 +359,9 @@ In the left-hand navigation pane, select the **Azure Active Directory** service,
333359
### Update the `TodoListClient-aspnetcore-webapi` to call the `TodoListService-aspnetcore-webapi` Running in Azure App Services
334360
335361
1. In your IDE, go to the `TodoListClient-aspnetcore-webapi` project.
336-
2. Open `TodoListClient\appsettings.json`. Only one change is needed - update the `todo:TodoListBaseAddress` key value to be the address of the website you published,
362+
1. Open `TodoListClient\appsettings.json`. Only one change is needed - update the `todo:TodoListBaseAddress` key value to be the address of the website you published,
337363
for example, [https://TodoListService-aspnetcore-webapi-contoso.azurewebsites.net](https://TodoListService-aspnetcore-webapi-contoso.azurewebsites.net).
338-
3. Run the client! If you are trying multiple different client types (for example, .Net, Windows Store, Android, iOS) you can have them all call this one published web API.
364+
1. Run the client! If you are trying multiple different client types (for example, .Net, Windows Store, Android, iOS) you can have them all call this one published web API.
339365
340366
### Create and publish `TodoListClient-aspnetcore-webapi` to an Azure App Services
341367
@@ -364,7 +390,7 @@ In the left-hand navigation pane, select the **Azure Active Directory** service,
364390
365391
## Next steps
366392
367-
If you're interested in the Web API calling a downstream API, you might want to have a look at the [ASP.NET Core Web API tutorial](https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2), in chapter 2 [2. Web API now calls Microsoft Graph/](https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2/tree/master/2.%20Web%20API%20now%20calls%20Microsoft%20Graph). The client is a desktop app there, whereas you have a Web App, but apart from that all the app registration steps apply.
393+
If you're interested in the Web API calling a downstream API, you might want to have a look at the [ASP.NET Core Web API tutorial](https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2), in chapter 2 [2. Web API now calls Microsoft Graph](https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2/tree/master/2.%20Web%20API%20now%20calls%20Microsoft%20Graph). The client is a desktop app there, whereas you have a Web App, but apart from that all the app registration steps apply.
368394
369395
## Community Help and Support
370396
@@ -384,25 +410,17 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
384410
385411
## More information
386412
387-
For more information, visit the following links:
388-
389-
- Articles about the Microsoft identity platform are at [https://aka.ms/aaddevv2](https://aka.ms/aaddevv2), with a focus on:
390-
- [Azure AD OAuth Bearer protocol](https://docs.microsoft.com/azure/active-directory/develop/active-directory-v2-protocols)
391-
- [The OAuth 2.0 protocol in Azure AD](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow)
392-
- [Access token](https://docs.microsoft.com/azure/active-directory/develop/access-tokens)
393-
- [The OpenID Connect protocol](https://docs.microsoft.com/azure/active-directory/develop/v2-protocols-oidc)
413+
Learn more about **Microsoft Identity Platform** and **Azure AD B2C**:
394414
395-
- To lean more about the application registration, visit:
396-
- [Quickstart: Register an application with the Microsoft identity platform (Preview)](https://docs.microsoft.com/azure/active-directory/develop/quickstart-register-app)
397-
- [Quickstart: Configure a client application to access web APIs (Preview)](https://docs.microsoft.com/azure/active-directory/develop/quickstart-configure-app-access-web-apis)
398-
- [Quickstart: Configure an application to expose web APIs (Preview)](https://docs.microsoft.com/azure/active-directory/develop/quickstart-configure-app-expose-web-apis)
415+
- [Microsoft identity platform (Azure Active Directory for developers)](https://docs.microsoft.com/azure/active-directory/develop/)
416+
- [Overview of Microsoft Authentication Library (MSAL)](https://docs.microsoft.com/azure/active-directory/develop/msal-overview)
417+
- [What is Azure Active Directory B2C?](https://docs.microsoft.com/azure/active-directory-b2c/overview)
418+
- [Azure AD B2C User Flows](https://docs.microsoft.com/azure/active-directory-b2c/user-flow-overview)
419+
- [Azure AD B2C Custom Policies](https://docs.microsoft.com/azure/active-directory-b2c/custom-policy-overview)
420+
- [Tutorial: Grant access to an ASP.NET web API using Azure Active Directory B2C](https://docs.microsoft.com/azure/active-directory-b2c/tutorial-web-api-dotnet?tabs=app-reg-ga)
399421
400-
- To learn more about the code, visit:
401-
- [Conceptual documentation for MSAL.NET](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki#conceptual-documentation) and in particular:
402-
- [Acquiring tokens with authorization codes on web apps](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Acquiring-tokens-with-authorization-codes-on-web-apps)
403-
- [Customizing Token cache serialization](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/token-cache-serialization)
422+
To learn more about the code, visit:
404423
405-
- To learn more about security in aspnetcore,
406-
- [Introduction to Identity on ASP.NET Core](https://docs.microsoft.com/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&tabs=visual-studio%2Caspnetcore2x)
407-
- [AuthenticationBuilder](https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.authentication.authenticationbuilder?view=aspnetcore-2.0)
408-
- [Azure Active Directory with ASP.NET Core](https://docs.microsoft.com/aspnet/core/security/authentication/azure-active-directory/?view=aspnetcore-2.1)
424+
- [Conceptual documentation for MSAL.NET](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki#conceptual-documentation) and in particular:
425+
- [Acquiring tokens with authorization codes on web apps](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Acquiring-tokens-with-authorization-codes-on-web-apps)
426+
- [Customizing Token cache serialization](https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/token-cache-serialization)

0 commit comments

Comments
 (0)