Skip to content

Commit f95596d

Browse files
author
Kalyan Krishna
committed
renames, minor refactoring and updates to readme
1 parent 35397bc commit f95596d

File tree

8 files changed

+89
-56
lines changed

8 files changed

+89
-56
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public IActionResult AccessDenied()
3333
return View();
3434
}
3535

36+
/// <summary>
37+
/// Fetches all the groups a user is assigned to. This method requires the signed-in user to be assigned to the 'DirectoryViewers' approle.
38+
/// </summary>
39+
/// <returns></returns>
3640
[AuthorizeForScopes(Scopes = new[] { GraphScopes.DirectoryReadAll })]
37-
[Authorize(Policy = AppPolicies.DirectoryViewersOnly)]
41+
[Authorize(Policy = AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired)]
3842
public async Task<IActionResult> Groups()
3943
{
4044
string[] scopes = new[] { GraphScopes.DirectoryReadAll };

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public class HomeController : Controller
1919
private readonly ITokenAcquisition tokenAcquisition;
2020
private readonly WebOptions webOptions;
2121

22-
public HomeController(ITokenAcquisition tokenAcquisition,
23-
IOptions<WebOptions> webOptionValue)
22+
public HomeController(ITokenAcquisition tokenAcquisition, IOptions<WebOptions> webOptionValue)
2423
{
2524
this.tokenAcquisition = tokenAcquisition;
2625
this.webOptions = webOptionValue.Value;
@@ -56,6 +55,23 @@ public async Task<IActionResult> Profile()
5655
return View();
5756
}
5857

58+
/// <summary>
59+
/// Fetches and displays all the users in this directory. This method requires the signed-in user to be assigned to the 'UserReaders' approle.
60+
/// </summary>
61+
/// <returns></returns>
62+
[AuthorizeForScopes(Scopes = new[] { GraphScopes.UserReadBasicAll })]
63+
[Authorize(Policy = AuthorizationPolicies.AssignmentToUserReaderRoleRequired)]
64+
public async Task<IActionResult> Users()
65+
{
66+
// Initialize the GraphServiceClient.
67+
Graph::GraphServiceClient graphClient = GetGraphServiceClient(new[] { GraphScopes.UserReadBasicAll });
68+
69+
var users = await graphClient.Users.Request().GetAsync();
70+
ViewData["Users"] = users.CurrentPage;
71+
72+
return View();
73+
}
74+
5975
private Graph::GraphServiceClient GetGraphServiceClient(string[] scopes)
6076
{
6177
return GraphServiceClientFactory.GetAuthenticatedGraphClient(async () =>
@@ -72,17 +88,5 @@ public IActionResult Error()
7288
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
7389
}
7490

75-
[AuthorizeForScopes(Scopes = new[] { GraphScopes.UserReadBasicAll })]
76-
[Authorize(Policy = AppPolicies.UserReadersOnly)]
77-
public async Task<IActionResult> Users()
78-
{
79-
// Initialize the GraphServiceClient.
80-
Graph::GraphServiceClient graphClient = GetGraphServiceClient(new[] { GraphScopes.UserReadBasicAll });
81-
82-
var users = await graphClient.Users.Request().GetAsync();
83-
ViewData["Users"] = users.CurrentPage;
84-
85-
return View();
86-
}
8791
}
8892
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@
66
namespace WebApp_OpenIDConnect_DotNet.Infrastructure
77
{
88
/// <summary>
9-
/// Contains a list of all the Azure Ad app roles this app works with
9+
/// Contains a list of all the Azure AD app roles this app depends on and works with.
1010
/// </summary>
11-
public static class AppRoles
11+
public static class AppRole
1212
{
13+
/// <summary>
14+
/// User readers can read basic profiles of all users in the directory.
15+
/// </summary>
1316
public const string UserReaders = "UserReaders";
17+
18+
/// <summary>
19+
/// Directory viewers can view objects in the whole directory.
20+
/// </summary>
1421
public const string DirectoryViewers = "DirectoryViewers";
1522
}
1623

17-
public static class AppPolicies
24+
/// <summary>
25+
/// Wrapper class the contain all the authorization policies available in this application.
26+
/// </summary>
27+
public static class AuthorizationPolicies
1828
{
19-
public const string UserReadersOnly = "UserReadersOnly";
20-
public const string DirectoryViewersOnly = "DirectoryViewersOnly";
29+
public const string AssignmentToUserReaderRoleRequired = "AssignmentToUserReaderRoleRequired";
30+
public const string AssignmentToDirectoryViewerRoleRequired = "AssignmentToDirectoryViewerRoleRequired";
2131
}
2232
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(this
176176
// Use the groups claim for populating roles
177177
options.TokenValidationParameters.RoleClaimType = "roles";
178178
});
179+
180+
// Adding authorization policies that enforce authorization using Azure AD roles.
181+
services.AddAuthorization(options =>
182+
{
183+
options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
184+
options.AddPolicy(AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired, policy => policy.RequireRole(AppRole.DirectoryViewers));
185+
});
179186
// [removed for] brevity
180187
}
181188

@@ -198,7 +205,7 @@ The following files have the code that would be of interest to you.
198205

199206
1. Startup.cs
200207

201-
1. In the `ConfigureServices` method of `Startup.cs', add the following line:
208+
1. In the `ConfigureServices` method of `Startup.cs', add the following lines:
202209

203210
```CSharp
204211
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
@@ -211,16 +218,23 @@ The following files have the code that would be of interest to you.
211218
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.
212219

213220
```CSharp
214-
[Authorize(Roles = AppRoles.UserReaders )]
221+
[Authorize(Policy = AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired)]
215222
public async Task<IActionResult> Users()
216223
{
217224
```
218225

219226
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:
220227

221228
```CSharp
222-
// The claim in the Jwt token where App roles are available.
223-
options.TokenValidationParameters.RoleClaimType = "roles";
229+
// The claim in the Jwt token where App roles are available.
230+
options.TokenValidationParameters.RoleClaimType = "roles";
231+
232+
// Adding authorization policies that enforce authorization using Azure AD roles.
233+
services.AddAuthorization(options =>
234+
{
235+
options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
236+
options.AddPolicy(AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired, policy => policy.RequireRole(AppRole.DirectoryViewers));
237+
});
224238
```
225239

226240
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.
@@ -234,7 +248,7 @@ The following files have the code that would be of interest to you.
234248
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.
235249

236250
```CSharp
237-
[Authorize(Roles = AppRoles.DirectoryViewers)]
251+
[Authorize(Policy = AuthorizationPolicies.AssignmentToUserReaderRoleRequired)]
238252
public async Task<IActionResult> Groups()
239253
{
240254
```

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ There is one project in this sample. To register it, you can:
100100
1. In PowerShell run:
101101

102102
```PowerShell
103-
cd .\AppCreationScripts\
103+
cd .\AppCreationScripts\
104104
.\Configure.ps1
105105
```
106106

@@ -251,16 +251,20 @@ public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(this
251251
// Use the groups claim for populating roles
252252
options.TokenValidationParameters.RoleClaimType = "roles";
253253
});
254+
255+
// Adding authorization policies that enforce authorization using Azure AD roles.
256+
services.AddAuthorization(options =>
257+
{
258+
options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
259+
options.AddPolicy(AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired, policy => policy.RequireRole(AppRole.DirectoryViewers));
260+
});
254261
// [removed for] brevity
255262
}
256263

257-
/*
258264
// In code..(Controllers & elsewhere)
259-
[Authorize(Policy = DirectoryViewersOnly")] // In controllers
265+
[Authorize(Policy = AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired)]
260266
// or
261-
User.IsInRole("UserReaders"); // In methods
262-
*/
263-
267+
User.IsInRole("UserReaders"); // In methods
264268
```
265269

266270
## About the code
@@ -306,33 +310,30 @@ This project was created using the following command.
306310
// 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles'
307311
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
308312
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
313+
314+
// Adding authorization policies that enforce authorization using Azure AD roles.
315+
services.AddAuthorization(options =>
316+
{
317+
options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
318+
options.AddPolicy(AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired, policy => policy.RequireRole(AppRole.DirectoryViewers));
319+
});
309320
```
310321

311-
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):
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:
312323
313-
```CSharp
314-
services.AddAuthorization(options =>
315-
{
316-
options.AddPolicy(AppPolicies.UserReadersOnly, policy => policy.RequireRole(AppRoles.UserReaders));
317-
options.AddPolicy(AppPolicies.DirectoryViewersOnly, policy => policy.RequireRole(AppRoles.DirectoryViewers));
318-
});
319-
```
324+
```CSharp
325+
// The claim in the Jwt token where App roles are available.
326+
options.TokenValidationParameters.RoleClaimType = "roles";
327+
```
320328
321-
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.
329+
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.
322330
323331
```CSharp
324-
[Authorize(Policy = AppPolicies.UserReadersOnly)]
332+
[Authorize(Policy = AuthorizationPolicies.AssignmentToUserReaderRoleRequired)]
325333
public async Task<IActionResult> Users()
326334
{
327335
```
328336
329-
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:
330-
331-
```CSharp
332-
// The claim in the Jwt token where App roles are available.
333-
options.TokenValidationParameters.RoleClaimType = "roles";
334-
```
335-
336337
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.
337338
338339
```CSharp
@@ -341,10 +342,10 @@ This project was created using the following command.
341342
{
342343
```
343344
344-
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.
345+
1. The following method is also added with the `Authorize` attribute with the name of the policy that enforces that the signed-in user is present in the app role **DirectoryViewers**, that permits listing of roles and groups the signed-in user is assigned to.
345346
346347
```CSharp
347-
[Authorize(Policy = AppPolicies.DirectoryViewersOnly)]
348+
[Authorize(Policy = AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired)]
348349
public async Task<IActionResult> Groups()
349350
{
350351
```
@@ -384,7 +385,7 @@ In the left-hand navigation pane, select the **Azure Active Directory** service,
384385
385386
## Next steps
386387
387-
- 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).
388+
- 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).
388389
389390
## Learn more
390391

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

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

64-
// Creating policies that wraps the authorization requirements
64+
// Adding authorization policies that enforce authorization using Azure AD roles.
6565
services.AddAuthorization(options =>
6666
{
67-
options.AddPolicy(AppPolicies.UserReadersOnly, policy => policy.RequireRole(AppRoles.UserReaders));
68-
options.AddPolicy(AppPolicies.DirectoryViewersOnly, policy => policy.RequireRole(AppRoles.DirectoryViewers));
67+
options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
68+
options.AddPolicy(AuthorizationPolicies.AssignmentToDirectoryViewerRoleRequired, policy => policy.RequireRole(AppRole.DirectoryViewers));
6969
});
7070

7171
services.AddControllersWithViews(options =>

5-WebApp-AuthZ/5-1-Roles/WebApp-OpenIDConnect-DotNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.0.0" />
22-
<PackageReference Include="Microsoft.Graph" Version="1.14.0" />
22+
<PackageReference Include="Microsoft.Graph" Version="1.21.0" />
2323
</ItemGroup>
2424

2525
<ItemGroup>

Microsoft.Identity.Web/Microsoft.Identity.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@
5656
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.0.0" />
5757
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="3.0.0" />
5858
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.1" />
59-
<PackageReference Include="Microsoft.Identity.Client" Version="4.7.1" />
59+
<PackageReference Include="Microsoft.Identity.Client" Version="4.8.0" />
6060
</ItemGroup>
6161
</Project>

0 commit comments

Comments
 (0)