Skip to content

Commit b510803

Browse files
author
Tiago Brenck
committed
Refactoring to use policy based authZ since is the current .NET recommendation
1 parent 66fadc9 commit b510803

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

5-WebApp-AuthZ/5-1-Roles/Controllers/AccountController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public IActionResult AccessDenied()
3434
}
3535

3636
[AuthorizeForScopes(Scopes = new[] { GraphScopes.DirectoryReadAll })]
37-
[Authorize(Roles = AppRoles.DirectoryViewers)]
37+
[Authorize(Policy = AppPolicies.DirectoryViewersOnly)]
3838
public async Task<IActionResult> Groups()
3939
{
4040
string[] scopes = new[] { GraphScopes.DirectoryReadAll };

5-WebApp-AuthZ/5-1-Roles/Controllers/HomeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public IActionResult Error()
7373
}
7474

7575
[AuthorizeForScopes(Scopes = new[] { GraphScopes.UserReadBasicAll })]
76-
[Authorize(Roles = AppRoles.UserReaders)]
76+
[Authorize(Policy = AppPolicies.UserReadersOnly)]
7777
public async Task<IActionResult> Users()
7878
{
7979
// Initialize the GraphServiceClient.

5-WebApp-AuthZ/5-1-Roles/Infrastructure/AppRoles.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ public static class AppRoles
1313
public const string UserReaders = "UserReaders";
1414
public const string DirectoryViewers = "DirectoryViewers";
1515
}
16+
17+
public static class AppPolicies
18+
{
19+
public const string UserReadersOnly = "UserReadersOnly";
20+
public const string DirectoryViewersOnly = "DirectoryViewersOnly";
21+
}
1622
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ service: Microsoft Graph
88
endpoint: Microsoft identity platform
99
---
1010

11-
# Add authorization using **app roles** & **roles** claims to an ASP.NET Core web app thats signs-in users with the Microsoft identity platform
11+
# Add authorization using **app roles** & **roles** claims to an ASP.NET Core web app that signs-in users with the Microsoft identity platform
1212

1313
## About this sample
1414

@@ -273,15 +273,19 @@ public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(this
273273
// [removed for] brevity
274274
}
275275

276+
/*
276277
// In code..(Controllers & elsewhere)
277-
[Authorize(Roles = DirectoryViewers")] // In controllers
278+
[Authorize(Roles = DirectoryViewers")] // In controllers
279+
[Authorize(Policy = DirectoryViewersOnly")] // In controllers
278280
// or
279-
User.IsInRole("USerReaders"); // In methods
281+
User.IsInRole("UserReaders"); // In methods
282+
*/
283+
280284
```
281285

282286
## About the code
283287

284-
1. In the `ConfigureServices` method of `Startup.cs', add the following line:
288+
1. In the `ConfigureServices` method of `Startup.cs'`, we added the following line to build the `ClaimsIdentity` object using the claims names in the token:
285289

286290
```CSharp
287291
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
@@ -291,15 +295,25 @@ User.IsInRole("USerReaders"); // In methods
291295
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
292296
```
293297

294-
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.
298+
1. Still in the `ConfigureServices` method of `Startup.cs'`, we created the policies that wraps the authorization requirements in it. It is a good practice to wrap your authorization rules in policies, even if it is just one role, because policies are easily expandable, support unit tests, can have multiple requirements, can be code based and [more](https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-3.1):
299+
300+
```CSharp
301+
services.AddAuthorization(options =>
302+
{
303+
options.AddPolicy(AppPolicies.UserReadersOnly, policy => policy.RequireRole(AppRoles.UserReaders));
304+
options.AddPolicy(AppPolicies.DirectoryViewersOnly, policy => policy.RequireRole(AppRoles.DirectoryViewers));
305+
});
306+
```
307+
308+
1. In the `HomeController.cs`, the following method is added with the `Authorize` attribute with the name of the policy created to check the app role **UserReaders**, that permits listing of users in the tenant.
295309

296310
```CSharp
297-
[Authorize(Roles = AppRoles.UserReaders )]
311+
[Authorize(Policy = AppPolicies.UserReadersOnly)]
298312
public async Task<IActionResult> Users()
299313
{
300314
```
301315

302-
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:
316+
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:
303317

304318
```CSharp
305319
// The claim in the Jwt token where App roles are available.
@@ -314,10 +328,10 @@ User.IsInRole("USerReaders"); // In methods
314328
{
315329
```
316330

317-
1. The following method is also added with the `Authorize` attribute with the name of the app role **DirectoryViewers**, that permits listing of roles and groups the signed-in user is assigned to.
331+
1. The following method is also added with the `Authorize` attribute with the name of the policy created to check the app role **DirectoryViewers**, that permits listing of roles and groups the signed-in user is assigned to.
318332

319333
```CSharp
320-
[Authorize(Roles = AppRoles.DirectoryViewers)]
334+
[Authorize(Policy = AppPolicies.DirectoryViewersOnly)]
321335
public async Task<IActionResult> Groups()
322336
{
323337
```
@@ -357,7 +371,7 @@ In the left-hand navigation pane, select the **Azure Active Directory** service,
357371

358372
## Next steps
359373

360-
- Learn how to use app groups. [Add authorization using security groups & groups claims to a Web app thats signs-in users with the Microsoft identity platform](../../5-WebApp-AuthZ/5-2-Groups/README.md).
374+
- Learn how to use app groups. [Add authorization using security groups & groups claims to a Web app that signs-in users with the Microsoft identity platform](../../5-WebApp-AuthZ/5-2-Groups/README.md).
361375

362376
## Learn more
363377

5-WebApp-AuthZ/5-1-Roles/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public void ConfigureServices(IServiceCollection services)
6161
options.TokenValidationParameters.RoleClaimType = "roles";
6262
});
6363

64+
// Creating policies that wraps the authorization requirements
65+
services.AddAuthorization(options =>
66+
{
67+
options.AddPolicy(AppPolicies.UserReadersOnly, policy => policy.RequireRole(AppRoles.UserReaders));
68+
options.AddPolicy(AppPolicies.DirectoryViewersOnly, policy => policy.RequireRole(AppRoles.DirectoryViewers));
69+
});
70+
6471
services.AddControllersWithViews(options =>
6572
{
6673
var policy = new AuthorizationPolicyBuilder()

0 commit comments

Comments
 (0)