asp.net-coreoauth-2.0azure-active-directoryauthorize-attribute

How to get the [Authorize(Roles = "RoleName")] into variable?


i am trying to save the role from the AuthorizeAttribute in a variable but i cant seem to figure out how. i want something like this. Note: the User/Roles is created from Azure Active Directory

private string CalculateRole()
{
   var role = authorize.role; 
   return role;
}

i searched all over and "closest" i got is this question asp.net identity get all roles of logged in user

but all i get back is a list of Claims I cant find any "roles".


Solution

  • [Authorize(Roles = "RoleName")] is used for access. We can specify the roles that have access to the requested resource using the Roles property of Authorize attribute. For example, [Authorize(Roles = "Admin")] allows us to access the action method to users who are member of "Admin" role.

    For the currently signed in user for an application, you can always find the Application Roles assigned to them from the Role claims available as part of the access token from Azure Active Directory.

    enter image description here

    For more information, here's a sample that uses OpenID Connect to sign-in users and use Azure AD Application Roles (app roles) for authorization. Also, you could use Microsoft Graph API to get the roles.