0

So the users authenticate using AAD, but I need to get the role they have been allocated in the Database.

I have tried adding this to my openIdConnectAuthenticationOptions in my Startup.Auth as suggested in some posts:

TokenValidationParameters = new TokenValidationParameters()
                    {
                        ValidateIssuer = false, // Simplification (see note below)
                        //RoleClaimType = System.Security.Claims.ClaimTypes.Role
                        RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"

} But no role is displayed when I check the claims while debugging. I assume this is because there is no login happening as it would when using SignInManager, so I tried doing an actual sign in after AAD authenticated successfully, as I have the user Id from the DB:

var user = db.Users.Where(x => x.Id == loggedInUserId).FirstOrDefault();

            var userForIdentity = UserManager.FindById(user.Id);

            if (user != null)
            {
                await SignInManager.SignInAsync(user, true, true);
            }

I thought that if I do the above after the AAD signin, that the role would be added to allow me to make use of User.IsInRole("Administrator") for example, but it doesnt seem to add it.

I have seen some posts that say that we can edit the manifest in Azure AD on the app that was registered, but I dont have access to the clients AAD.

My question is, is there a way to make use of User.IsInRole("") based on what is in the DB after AAD sign in ?

Thanks for any help.

AxleWack
  • 1,801
  • 1
  • 19
  • 51
  • i believe youll need to add the needed claim manually after the azure ad sign in – GregH Jun 12 '19 at 15:33
  • That makes sense and I am trying to see if I can figure out how. Just not sure how I would add the claim from the Startup.Auth and then specify the role to be added to that claim that is linked to the logged in user - Do you have any idea how to do this ? – AxleWack Jun 12 '19 at 15:35
  • askh's answer here should help you considerably https://stackoverflow.com/questions/45881157/asp-net-core-add-role-claim-to-user – GregH Jun 12 '19 at 15:37

0 Answers0