Reputation: 693
I am trying to get the Roles of a logged in user , using using openID connect with "https://login.microsoftonline.com/"
I can hit breakpoints on OnTokenValidated , and OnTicketReceived . in my controller decorated with [Authorize], User.Identity.IsAuthenticated is true ,but User.IsInRole("admin") is false <-- this is a group in my tenant, and my account is a member. Any suggestions on getting role Info? Do i need to implement OnUserInformationReceived ? Any examples?
I am following the wiki : Quickstart: Add sign-in with Microsoft to an ASP.NET Core web app
Thanks,Peter
Upvotes: 0
Views: 1318
Reputation: 27538
Group membership is not included in ID token by default , you can follow below steps to configure your application to receive group claims :
Manifest
to open the inline manifest editor.groupMembershipClaims
setting, and setting its value to All
(or to SecurityGroup
if you are not interested in Distribution Lists).Save the manifest.
{
...
"createdDateTime": "",
"groupMembershipClaims": "All",
"identifierUris": [],
...
}
You could use policy in asp.net core , use an attribute with a named policy then you define the policy in startup to require group claim and set allowed Group ID . See code sample here .
In addition, User.IsInRole("admin")
will check whether http://schemas.microsoft.com/ws/2008/06/identity/claims/role: admin
exists in user claims . You can also set the RoleClaimType to use group information and then use Roles .
Upvotes: 2