Skip to content

Commit 8968223

Browse files
committed
Minor Updates
1 parent 1c4b6a2 commit 8968223

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed

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

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

55
### Quick summary
66

7-
1. On Windows run PowerShell and navigate to the root of the cloned directory
7+
1. On Windows run PowerShell as **Administrator** and navigate to the root of the cloned directory
88
1. In PowerShell run:
99

1010
```PowerShell
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace WebApp_OpenIDConnect_DotNet.Controllers
5+
{
6+
public class AccountController : Controller
7+
{
8+
[Authorize]
9+
public IActionResult SignOut()
10+
{
11+
HttpContext.Session.Clear();
12+
return RedirectToAction("SignOut", "Account", new { area = "MicrosoftIdentity" });
13+
}
14+
}
15+
}

5-WebApp-AuthZ/5-2-Groups/Infrastructure/CustomAuthorization.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte
3434
{
3535
// Retrieves all the groups saved in Session.
3636
var groups = _httpContextAccessor.HttpContext.Session.GetAsByteArray("groupClaims") as List<string>;
37-
groups = null;
3837

3938
// Checks if required group exists in Session.
4039
if (groups?.Count > 0 && groups.Contains(requirement.GroupName))

5-WebApp-AuthZ/5-2-Groups/Services/MicrosoftGraph-Rest/GraphHelper.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,25 @@ public static async Task<List<string>> GetSignedInUsersGroups(TokenValidatedCont
7575

7676
if (identity != null)
7777
{
78-
// Re-populate the `groups` claim with the complete list of groups fetched from MS Graph
79-
foreach (Group group in allgroups)
78+
// Checks if token is 'ID Token'.
79+
// ID Token does not contain 'aapid' or 'azp' claims.
80+
// These claims exist for Access Token.
81+
if (!identity.Claims.Any(x => x.Type == "appid" || x.Type == "azp"))
8082
{
81-
// The following code adds group ids to the 'groups' claim. But depending upon your reequirement and the format of the 'groups' claim selected in
82-
// the app registration, you might want to add other attributes than id to the `groups` claim, examples being;
83+
// Re-populate the `groups` claim with the complete list of groups fetched from MS Graph
84+
foreach (Group group in allgroups)
85+
{
86+
// The following code adds group ids to the 'groups' claim. But depending upon your reequirement and the format of the 'groups' claim selected in
87+
// the app registration, you might want to add other attributes than id to the `groups` claim, examples being;
8388

84-
// For instance if the required format is 'NetBIOSDomain\sAMAccountName' then the code is as commented below:
85-
// groupClaims.Add(group.OnPremisesNetBiosName+"\\"+group.OnPremisesSamAccountName));
86-
groupClaims.Add(group.Id);
87-
}
89+
// For instance if the required format is 'NetBIOSDomain\sAMAccountName' then the code is as commented below:
90+
// groupClaims.Add(group.OnPremisesNetBiosName+"\\"+group.OnPremisesSamAccountName));
91+
groupClaims.Add(group.Id);
92+
}
8893

89-
// Here we add the groups in a session variable that is used in authorization policy handler.
90-
context.HttpContext.Session.SetAsByteArray("groupClaims", groupClaims);
94+
// Here we add the groups in a session variable that is used in authorization policy handler.
95+
context.HttpContext.Session.SetAsByteArray("groupClaims", groupClaims);
96+
}
9197
}
9298
}
9399
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public void ConfigureServices(IServiceCollection services)
4949
//Calls method to process groups overage claim.
5050
var groupClaims = await GraphHelper.GetSignedInUsersGroups(context);
5151
};
52-
options.Events.OnSignedOutCallbackRedirect = async context =>
53-
{
54-
context.HttpContext.Session.Clear();
55-
};
5652
}, options => { Configuration.Bind("AzureAd", options); })
5753
.EnableTokenAcquisitionToCallDownstreamApi(options => Configuration.Bind("AzureAd", options), initialScopes)
5854
.AddMicrosoftGraph(Configuration.GetSection("GraphAPI"))

5-WebApp-AuthZ/5-2-Groups/Views/Shared/_LoginPartial.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
<ul class="nav navbar-nav navbar-right">
55
<li>@Html.ActionLink(User.Identity.Name + "!", "Index", "UserProfile", routeValues: null)</li>
6-
<li><a asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Sign out</a></li>
6+
<li><a asp-controller="Account" asp-action="SignOut">Sign out</a></li>
77
</ul>
88
}
99
else

0 commit comments

Comments
 (0)