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
> 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.
19
17
20
18
## Overview
21
19
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.
23
21
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.
26
24
27
25
The client web application essentially takes the following steps to sign-in the user and obtain a bearer token for the Web API:
28
26
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.
30
28
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.
32
30
33
31

34
32
@@ -40,7 +38,6 @@ This sample has a web API and a client web app, both built using the asp.net cor
- 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.
44
41
45
42
## Setup
46
43
@@ -79,7 +76,7 @@ Please refer to: [Tutorial: Add identity providers to your applications in Azure
79
76
80
77
### Register the service app (TodoListService-aspnetcore-webapi)
81
78
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.
83
80
84
81
#### Configure the service app (TodoListService-aspnetcore-webapi) to use your app registration
85
82
@@ -114,17 +111,44 @@ Open the project in your IDE (like Visual Studio or Visual Studio Code) to confi
114
111
115
112
## Running the sample
116
113
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
120
123
121
124
```console
122
-
cd TodoListService-aspnetcore-webapi
123
-
dotnet run
125
+
cd TodoListService
126
+
dotnet restore
124
127
```
125
128
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
+
126
151
```console
127
-
cd TodoListClient-aspnetcore-webapi
128
152
dotnet run
129
153
```
130
154
@@ -138,9 +162,9 @@ When you start the Web API from Visual Studio, depending on the browser you use,
138
162
- an empty web page (case with Microsoft Edge)
139
163
- or an error HTTP 401 (case with Chrome)
140
164
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.
142
166
143
-
Explore the sample by signing in into the TodoList client, adding items to the ToDo 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.
144
168
145
169
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.
146
170
@@ -194,18 +218,16 @@ NOTE: Remember, the To-Do list is stored in memory in this `TodoListService` app
194
218
195
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.
196
220
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:
198
222
- update the `sslPort` of the `iisSettings` section to be `44321`
199
223
- 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
202
225
203
226
```CSharp
204
-
// Add APIs
205
227
services.AddTodoListService(Configuration);
206
228
```
207
229
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.
209
231
210
232
#### Add a model (TodoListItem) and add the controller and views
211
233
@@ -223,24 +245,27 @@ NOTE: Remember, the To-Do list is stored in memory in this `TodoListService` app
223
245
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Configuration["TodoList:TodoListScope"] }, configSectionName: "AzureAdB2C");
224
246
services.AddInMemoryTokenCaches();
225
247
```
248
+
226
249
1. Update the `Configure` method to include **app.UseAuthentication();** before **app.UseMvc();**
227
250
228
-
```Csharp
229
-
app.UseAuthentication();
230
-
app.UseMvc();
231
-
```
251
+
```Csharp
252
+
app.UseAuthentication();
253
+
app.UseMvc();
254
+
```
232
255
233
256
### Creating the Web API project (TodoListService)
234
257
235
258
The code forthe TodoListService was createdin the following way:
236
259
237
260
#### Step 1: Create the web api using the ASP.NET Core templates
238
261
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
+
```
244
269
245
270
1. Open the generated project (.csproj) in Visual Studio, and save the solution.
246
271
@@ -280,6 +305,7 @@ using Microsoft.Identity.Web.Client.TokenCacheProviders;
- Add the method **app.UseAuthentication()** before **app.UseMvc()**in the `Configure` method
284
310
285
311
```Csharp
@@ -333,9 +359,9 @@ In the left-hand navigation pane, select the **Azure Active Directory** service,
333
359
### Update the `TodoListClient-aspnetcore-webapi` to call the `TodoListService-aspnetcore-webapi` Running in Azure App Services
334
360
335
361
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,
337
363
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.
339
365
340
366
### Create and publish `TodoListClient-aspnetcore-webapi` to an Azure App Services
341
367
@@ -364,7 +390,7 @@ In the left-hand navigation pane, select the **Azure Active Directory** service,
364
390
365
391
## Next steps
366
392
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.
368
394
369
395
## Community Help and Support
370
396
@@ -384,25 +410,17 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
384
410
385
411
## More information
386
412
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)
Learn more about **Microsoft Identity Platform** and **Azure AD B2C**:
394
414
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)
- [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)
- [Introduction to Identity on ASP.NET Core](https://docs.microsoft.com/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&tabs=visual-studio%2Caspnetcore2x)
- [Azure Active Directory with ASP.NET Core](https://docs.microsoft.com/aspnet/core/security/authentication/azure-active-directory/?view=aspnetcore-2.1)
- [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)
0 commit comments