【问题标题】:How to make [Authorize(Roles = "Administrator")] work in mvc ASP.NET Core如何使 [Authorize(Roles = "Administrator")] 在 mvc ASP.NET Core 中工作
【发布时间】:2019-11-02 03:19:57
【问题描述】:

我正在 ASP.NET Core(实体框架)中准备应用程序,我希望将某些功能限制为特定用户。

为了在 Startup.cs 中实现这一点,我添加了以下代码 服务:

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

创建新默认用户的类:

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;
        }
    }
}

}

在配置方法中我调用这个类:

MyIdentityDataInitializer.SeedData(userManager, roleManager); 

在我喜欢限制动作的控制器中

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

目前的情况是:

我可以在数据库中检查是否创建了用户和角色(数据库 ASPNetUser、ASPNetRoles),也在数据库 ASPNetUserRoles 中存在用户映射为管理员的行。 应用程序启动后,我可以登录,但是当我尝试打开 ConfigurationPortal()(或其他受限方法)时,会显示访问受限信息。看起来管理员用户未被识别为管理员。

预期的情况是;当用户附加到角色管理员时,他们可以访问受限方法。

【问题讨论】:

    标签: asp.net entity-framework asp.net-core authorization


    【解决方案1】:

    应用程序启动后,我可以登录,但当我尝试打开 ConfigurationPortal()(或其他受限方法)时,会显示访问受限信息。管理员用户似乎未被识别为管理员。

    这是 2.1 版本中的一个已知错误。请在此处查看issue

    我听从了using the old api suggested by HaoK and C-BERBER 的建议,现在可以完美运行了。

    这是我的 DbContext:

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

    使用旧式 api 配置身份:

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

    最后,注销并重新登录,现在可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2023-02-02
      • 2021-01-11
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多