Skip to content

Commit 643d69b

Browse files
committed
2 parents aab6942 + 66ee974 commit 643d69b

File tree

9 files changed

+76
-47
lines changed

9 files changed

+76
-47
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"secrets1": {
4+
"type": "secrets"
5+
}
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"secrets1": {
4+
"type": "secrets.user"
5+
}
6+
}
7+
}

2-WebApp-graph-user/2-2-TokenCache/Startup.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ public void ConfigureServices(IServiceCollection services)
4747
// NOTE : This is a one time use method. We advise using it in development environments to create the tables required to enable token caching.
4848
// For production deployments, preferably, generate the schema from the tables generated in dev environments and use it to create the necessary tables in production.
4949
/*
50-
dotnet tool install --global dotnet-sql-cache
51-
dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MsalTokenCacheDatabase;Integrated Security=True;" dbo TokenCache
50+
* 1. For instance in Visual Studio, open the SQL Server Object explorer, then (localdb)\MSSQLLocalDB, then databases
51+
* 2. Right click on Databases and select "Add New database", and then choose the name of the database: 'MsalTokenCacheDatabase'
52+
* 3. In the console application run the 2 following commands:
53+
dotnet tool install --global dotnet-sql-cache
54+
dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MsalTokenCacheDatabase;Integrated Security=True;" dbo TokenCache
5255
*/
5356

5457
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)

4-WebApp-your-API/4-2-B2C/TodoListService/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public void ConfigureServices(IServiceCollection services)
3434
// JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
3535

3636
// Adds Microsoft Identity platform (AAD v2.0) support to protect this Api
37-
services.AddAuthentication()
37+
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
3838
.AddMicrosoftWebApi(options =>
3939
{
4040
Configuration.Bind("AzureAdB2C", options);
4141

4242
options.TokenValidationParameters.NameClaimType = "name";
4343
},
44-
options => { Configuration.Bind("AzureAdB2C", options); });
44+
options => { Configuration.Bind("AzureAdB2C", options); });
4545

4646
services.AddControllers();
4747
services.AddAuthorization(options =>

5-WebApp-AuthZ/5-1-Roles/README-incremental-instructions.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ When you click on the page that fetches the signed-in user's roles and group ass
154154
155155
### Support in ASP.NET Core middleware libraries
156156

157-
The asp.net middleware supports roles populated from claims by specifying the claim in the `RoleClaimType` property of `TokenValidationParameters`.
157+
The ASP.NET middleware supports roles populated from claims by specifying the claim in the `RoleClaimType` property of `TokenValidationParameters`.
158158

159159
```CSharp
160160

161161
// Startup.cs
162-
public static IServiceCollection AddMicrosoftWebApp(this IServiceCollection services, IConfiguration configuration, X509Certificate2 tokenDecryptionCertificate = null)
162+
public void ConfigureServices(IServiceCollection services)
163163
{
164164
// [removed for brevity]
165165
@@ -171,7 +171,7 @@ public static IServiceCollection AddMicrosoftWebApp(this IServiceCollection serv
171171

172172
// The following lines code instruct the asp.net core middleware to use the data in the "groups" claim in the Authorize attribute and User.IsInrole()
173173
// See https://docs.microsoft.com/aspnet/core/security/authorization/roles?view=aspnetcore-2.2 for more info.
174-
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
174+
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
175175
{
176176
// Use the groups claim for populating roles
177177
options.TokenValidationParameters.RoleClaimType = "roles";
@@ -183,7 +183,8 @@ public static IServiceCollection AddMicrosoftWebApp(this IServiceCollection serv
183183
options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
184184
options.AddPolicy(AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired, policy => policy.RequireRole(AppRole.DirectoryViewers));
185185
});
186-
// [removed for brevity]
186+
187+
// [removed for brevity]
187188
}
188189

189190
// In code..(Controllers & elsewhere)
@@ -198,19 +199,14 @@ The following files have the code that would be of interest to you.
198199

199200
1. HomeController.cs
200201
1. Passes the **HttpContext.User** (the signed-in user) to the view.
201-
1 Services\GraphServiceClientFactory.cs
202+
1. Services\GraphServiceClientFactory.cs
202203
1. Uses the [Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-dotnet) to carry out various operations with [Microsoft Graph](https://graph.microsoft.com).
203204
1. Home\Index.cshtml
204205
1. This has some code to print the current user's claims
205206

206-
1. Startup.cs
207-
208207
1. In the `ConfigureServices` method of `Startup.cs', add the following lines:
209208

210209
```CSharp
211-
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
212-
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
213-
214210
//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.
215211
services.AddMicrosoftWebAppAuthentication(Configuration)
216212
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Constants.ScopeUserRead })
@@ -225,19 +221,15 @@ The following files have the code that would be of interest to you.
225221
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
226222
```
227223

228-
1. In the `HomeController.cs`, the following method is added with the `Authorize` attribute with the name of the app role **UserReaders**, that permits listing of users in the tenant.
229-
230-
```CSharp
231-
[Authorize(Policy = AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired)]
232-
public async Task<IActionResult> Users()
233-
{
234-
```
235-
236-
1. In the `ConfigureServices` method of `Startup.cs', the following line instructs the asp.net security middleware to use the **roles** claim to fetch roles for authorization:
224+
1. In the `ConfigureServices` method of `Startup.cs', the following lines instruct the ASP.NET security middleware to use the **roles** claim to fetch roles for authorization:
237225

238226
```CSharp
239-
// The claim in the Jwt token where App roles are available.
240-
options.TokenValidationParameters.RoleClaimType = "roles";
227+
// Add this configuration after the call to `AddMicrosoftWebAppAuthentication`.
228+
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
229+
{
230+
// The claim in the JWT token where App roles are available.
231+
options.TokenValidationParameters.RoleClaimType = "roles";
232+
});
241233

242234
// Adding authorization policies that enforce authorization using Azure AD roles.
243235
services.AddAuthorization(options =>
@@ -247,6 +239,14 @@ The following files have the code that would be of interest to you.
247239
});
248240
```
249241

242+
1. In the `HomeController.cs`, the following method is added with the `Authorize` attribute with the name of the app role **UserReaders**, that permits listing of users in the tenant.
243+
244+
```CSharp
245+
[Authorize(Policy = AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired)]
246+
public async Task<IActionResult> Users()
247+
{
248+
```
249+
250250
1. A new class called `AccountController.cs` is introduced. This contains the code to intercept the default AccessDenied error's route and present the user with an option to sign-out and sign-back in with a different account that has access to the required role.
251251

252252
```CSharp

5-WebApp-AuthZ/5-1-Roles/README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ When you click on the page that fetches the signed-in user's roles and group ass
229229
230230
### Support in ASP.NET Core middleware libraries
231231

232-
The asp.net middleware supports roles populated from claims by specifying the claim in the `RoleClaimType` property of `TokenValidationParameters`.
232+
The ASP.NET middleware supports roles populated from claims by specifying the claim in the `RoleClaimType` property of `TokenValidationParameters`.
233233

234234
```CSharp
235235

236236
// Startup.cs
237-
public static IServiceCollection AddMicrosoftWebApp(this IServiceCollection services, IConfiguration configuration, X509Certificate2 tokenDecryptionCertificate = null)
237+
public void ConfigureServices(IServiceCollection services)
238238
{
239239
// [removed for] brevity
240240
@@ -246,7 +246,7 @@ public static IServiceCollection AddMicrosoftWebApp(this IServiceCollection serv
246246

247247
// The following lines code instruct the asp.net core middleware to use the data in the "groups" claim in the Authorize attribute and User.IsInrole()
248248
// See https://docs.microsoft.com/aspnet/core/security/authorization/roles?view=aspnetcore-2.2 for more info.
249-
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
249+
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
250250
{
251251
// Use the groups claim for populating roles
252252
options.TokenValidationParameters.RoleClaimType = "roles";
@@ -258,7 +258,8 @@ public static IServiceCollection AddMicrosoftWebApp(this IServiceCollection serv
258258
options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
259259
options.AddPolicy(AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired, policy => policy.RequireRole(AppRole.DirectoryViewers));
260260
});
261-
// [removed for] brevity
261+
262+
// [removed for] brevity
262263
}
263264

264265
// In code..(Controllers & elsewhere)
@@ -286,14 +287,14 @@ This project was created using the following command.
286287
1. Add a reference from your newly generated project to `Microsoft.Identity.Web` (right click on the **Dependencies** node under your new project, and choose **Add Reference ...**, and then in the projects tab find the `Microsoft.Identity.Web` project)
287288
1. Open the **Startup.cs** file and:
288289
289-
- in the `ConfigureServices` method, the following lines have been replaced :
290+
- in the `ConfigureServices` method, the following lines:
290291
291292
```CSharp
292293
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
293294
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
294-
295-
// by these lines:
296-
295+
```
296+
have been replaced by these lines:
297+
```CSharp
297298
//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.
298299
services.AddMicrosoftWebAppAuthentication(Configuration)
299300
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { Constants.ScopeUserRead })
@@ -302,7 +303,7 @@ This project was created using the following command.
302303
services.AddGraphService(Configuration); // Adds the IMSGraphService as an available service for this app.
303304
```
304305
305-
1. In the `ConfigureServices` method of `Startup.cs', add the following line:
306+
1. In the `ConfigureServices` method of `Startup.cs', add the following lines:
306307

307308
```CSharp
308309
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
@@ -319,11 +320,15 @@ This project was created using the following command.
319320
});
320321
```
321322

322-
1. In the `ConfigureServices` method of `Startup.cs', the following line instructs the asp.net security middleware to use the **roles** claim to fetch roles for authorization:
323+
1. In the `ConfigureServices` method of `Startup.cs', the following lines instruct the ASP.NET security middleware to use the **roles** claim to fetch roles for authorization.
323324
324325
```CSharp
325-
// The claim in the Jwt token where App roles are available.
326-
options.TokenValidationParameters.RoleClaimType = "roles";
326+
// Add this configuration after the call to `AddMicrosoftWebAppAuthentication`.
327+
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
328+
{
329+
// The claim in the JWT token where App roles are available.
330+
options.TokenValidationParameters.RoleClaimType = "roles";
331+
});
327332
```
328333
329334
1. In the `HomeController.cs`, the following method is added with the `Authorize` attribute with the name of the policy that enforces that the signed-in user is present in the app role **UserReaders**, that permits listing of users in the tenant.

5-WebApp-AuthZ/5-2-Groups/README-incremental-instructions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ The following files have the code that would be of interest to you:
218218
1. Passes the **HttpContext.User** (the signed-in user) to the view.
219219
1. UserProfileController.cs
220220
1. Uses the **IMSGraphService** methods to fetch the signed-in user's group memberships.
221-
1 IMSGraphService.cs, MSGraphService.cs and UserGroupsAndDirectoryRoles.cs
221+
1. IMSGraphService.cs, MSGraphService.cs and UserGroupsAndDirectoryRoles.cs
222222
1. Uses the [Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-dotnet) to carry out various operations with [Microsoft Graph](https://graph.microsoft.com).
223223
1. Home\Index.cshtml
224224
1. This has some code to print the current user's claims
@@ -232,14 +232,14 @@ The following files have the code that would be of interest to you:
232232
using Microsoft.Identity.Web;
233233
```
234234

235-
- in the `ConfigureServices` method, the following lines have been replaced :
235+
- in the `ConfigureServices` method, the following lines:
236236

237237
```CSharp
238238
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
239239
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
240240
```
241241

242-
- by these lines:
242+
- have been replaced by these lines::
243243

244244
```CSharp
245245
services.AddMicrosoftWebAppAuthentication(Configuration)

5-WebApp-AuthZ/5-2-Groups/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ The object id of the security groups the signed in user is member of is returned
232232

233233
### Support in ASP.NET Core middleware libraries
234234

235-
The asp.net middleware supports roles populated from claims by specifying the claim in the `RoleClaimType` property of `TokenValidationParameters`.
236-
Since the `groups` claim contains the object ids of the security groups than actual names by default, you'd use the group id's instead of group names. See [Role-based authorization in ASP.NET Core](https://docs.microsoft.com/aspnet/core/security/authorization/roles) for more info.
235+
The ASP.NET middleware supports roles populated from claims by specifying the claim in the `RoleClaimType` property of `TokenValidationParameters`.
236+
Since the `groups` claim contains the object IDs of the security groups than actual names by default, you'd use the group ID's instead of group names. See [Role-based authorization in ASP.NET Core](https://docs.microsoft.com/aspnet/core/security/authorization/roles) for more info.
237237

238238
```CSharp
239239
// Startup.cs
@@ -328,7 +328,7 @@ The following files have the code that would be of interest to you:
328328
1. Passes the **HttpContext.User** (the signed-in user) to the view.
329329
1. UserProfileController.cs
330330
1. Uses the **IMSGraphService** methods to fetch the signed-in user's group memberships.
331-
1 IMSGraphService.cs, MSGraphService.cs and UserGroupsAndDirectoryRoles.cs
331+
1. IMSGraphService.cs, MSGraphService.cs and UserGroupsAndDirectoryRoles.cs
332332
1. Uses the [Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-dotnet) to carry out various operations with [Microsoft Graph](https://graph.microsoft.com).
333333
1. Home\Index.cshtml
334334
1. This has some code to print the current user's claims
@@ -342,16 +342,16 @@ The following files have the code that would be of interest to you:
342342
using Microsoft.Identity.Web;
343343
```
344344

345-
- in the `ConfigureServices` method, the following lines have been replaced :
345+
- in the `ConfigureServices` method, the following lines:
346346

347347
```CSharp
348348
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
349349
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
350350
```
351351

352-
- by these lines:
353-
354-
```CSharp
352+
- have been replaced by these lines:
353+
-
354+
```CSharp
355355
services.AddMicrosoftWebAppAuthentication(Configuration)
356356
.AddMicrosoftWebAppCallsWebApi(Configuration, new string[] { "User.Read", "Directory.Read.All" })
357357
.AddInMemoryTokenCaches();

5-WebApp-AuthZ/5-2-Groups/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ public void ConfigureServices(IServiceCollection services)
5252

5353
services.AddMSGraphService(Configuration);
5454

55+
services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options => {
56+
// Uncomment the following lines code instruct the asp.net core middleware to use the data in the "groups" claim in the [Authorize] attribute and for User.IsInrole()
57+
// See https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles for more info.
58+
// Use the groups claim for populating roles
59+
options.TokenValidationParameters.RoleClaimType = "groups";
60+
});
61+
5562
services.AddControllersWithViews(options =>
5663
{
5764
var policy = new AuthorizationPolicyBuilder()

0 commit comments

Comments
 (0)