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
Copy file name to clipboardExpand all lines: 5-WebApp-AuthZ/5-1-Roles/README.md
+24-10Lines changed: 24 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ service: Microsoft Graph
8
8
endpoint: Microsoft identity platform
9
9
---
10
10
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
12
12
13
13
## About this sample
14
14
@@ -273,15 +273,19 @@ public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(this
273
273
// [removed for] brevity
274
274
}
275
275
276
+
/*
276
277
// In code..(Controllers & elsewhere)
277
-
[Authorize(Roles=DirectoryViewers")] // In controllers
278
+
[Authorize(Roles = DirectoryViewers")] // In controllers
279
+
[Authorize(Policy = DirectoryViewersOnly")] // In controllers
278
280
// or
279
-
User.IsInRole("USerReaders"); // In methods
281
+
User.IsInRole("UserReaders"); // In methods
282
+
*/
283
+
280
284
```
281
285
282
286
## About the code
283
287
284
-
1. Inthe `ConfigureServices` methodof `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:
285
289
286
290
```CSharp
287
291
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
@@ -291,15 +295,25 @@ User.IsInRole("USerReaders"); // In methods
1. Stillinthe `ConfigureServices` methodof `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):
1. Inthe `ConfigureServices` methodof `Startup.cs', the following line instructs the asp.net security middleware to use the **roles** claim to fetch roles for authorization:
316
+
1. Inthe `ConfigureServices` methodof `Startup.cs'`, the following line instructs the asp.net security middleware to use the **roles** claim to fetch roles for authorization:
303
317
304
318
```CSharp
305
319
// The claim in the Jwt token where App roles are available.
@@ -314,10 +328,10 @@ User.IsInRole("USerReaders"); // In methods
0 commit comments