简体   繁体   中英

How to make [Authorize(Roles = “Administrator”)] work in mvc ASP.NET Core

I'm preparing application in ASP.NET Core (Entity framework), where I would like to have some functions restricted to specific users.

To achieve that in Startup.cs I add following code service:

services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole> 
().AddEntityFrameworkStores<ApplicationDbContext>();

Class for create new default users:

public static class MyIdentityDataInitializer
{
    public static void SeedData(UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager) 
    {
        SeedRoles(roleManager);
        SeedUsers(userManager);
    }

    public static void SeedUsers(UserManager<IdentityUser> userManager)
    {
        if (userManager.FindByNameAsync("user@email.com").Result==null)
        {
            IdentityUser user = new IdentityUser();
            user.UserName = "user@email.com";
            user.Email = "user@email.com";
            IdentityResult result = userManager.CreateAsync(user, "Admin1").Result;  //Admin1 = password

            if (result.Succeeded)
            {
                userManager.AddToRoleAsync(user, "Administrator").Wait(); //add user to role
            }
        }

    }

    public static void SeedRoles(RoleManager<IdentityRole> roleManager)
    {
        if (!roleManager.RoleExistsAsync("StandardUser").Result)
        {
            IdentityRole role = new IdentityRole();
            role.Name = "StandardUser";
            IdentityResult roleResult = roleManager.CreateAsync(role).Result;
        }

        if (!roleManager.RoleExistsAsync("Administrator").Result)
        {
            IdentityRole role = new IdentityRole();
            role.Name = "Administrator";
            role.NormalizedName = "Administrator";
            IdentityResult roleResult = roleManager.CreateAsync(role).Result;
        }
    }
}

}

and in Configure metod i call this class:

MyIdentityDataInitializer.SeedData(userManager, roleManager); 

In controller where i like to restrict action

 [Authorize(Roles = "Administrator")]
    public IActionResult ConfigurationPortal()
    {
     .....
    }

Current situation is:

I can check in the database that User and Roles are created (databases ASPNetUser, ASPNetRoles), also in the database ASPNetUserRoles exist line where user is mapped as administrator. After application is started I can login, but when i try to open ConfigurationPortal() (or other restricted method) Access Restricted information is displayed. It looks like the Administrator user is not recognized as administrator.

The expected situation is; when a user is attached to role administrator they can access restricted methods.

After application is started I can login, but when i try to open ConfigurationPortal() (or other restricted method) Access Restricted information is displayed. It looks like the Administrator user is not recognized as administrator.

It's a known bug in the version of 2.1 . See issue here .

I follow the advice of using the old api suggested by HaoK and C-BERBER , and it now works flawlessly .

Here's my DbContext:

  public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

Configure the identity using the old-style api :

services.AddIdentity<IdentityUser, IdentityRole>()
        .AddRoleManager<RoleManager<IdentityRole>>()
        .AddDefaultUI()
        .AddDefaultTokenProviders()
        .AddEntityFrameworkStores<ApplicationDbContext>();

Lastly , logout and re-signin , it will work as expected now .

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