简体   繁体   中英

ASP.Net Core MVC Web application roles management

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM