ASP.Net Core MVC Web application roles management

178 views Asked by At

I have a ASP.NET core web application which i have hosted in Azure portal. I wanted to add authentication to application and also wanted to have different roles, where some users will have only view and some users will have edit permission in web application.

To achieve this i have added users and groups to application as mentioned here To handle roles who can view and edit etc, i have created roles in manifest and also added this roles to few users as mentioned here

Now to read this user roles in asp.net core mvc web app what i need to use? I saw articles suggesting Graphs API, Open ID etc. So what is the best approach here.

1

There are 1 answers

0
Allen Wu On BEST ANSWER

You can add authorization policies that enforce authorization using Azure AD roles in the Startup.cs file. (here UserReaders is the app role)

services.AddAuthorization(options => 
{
    options.AddPolicy(AuthorizationPolicies.AssignmentToUserReaderRoleRequired, policy => policy.RequireRole(AppRole.UserReaders));
});

And use the following code to check if the user has the role.

[Authorize(Policy = AuthorizationPolicies.AssignmentToUserReaderRoleRequired)]
// or
User.IsInRole("UserReaders"); 

See details from the ASP.NET Core web app sample.