【问题标题】:Checking roles with ASP:Net Identity 2.1 does not work使用 ASP:Net Identity 2.1 检查角色不起作用
【发布时间】:2018-10-07 07:38:34
【问题描述】:

我将 ASP.Net MVC Core 应用程序从 1.1 升级到 2.1,包括将 ASP.Net Identity 从 1.1 迁移到 2.1。

我得到了工作,包括使用 Sqlite 进行 ASP.Net Identity EntityFramework 集成。

我的startup.cs 配置看起来像this

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

        services.Configure<IdentityOptions>(options =>
        {
            // Password settings.
            options.Password.RequireDigit = true;
            options.Password.RequireLowercase = true;
            options.Password.RequireNonAlphanumeric = true;
            options.Password.RequireUppercase = true;
            options.Password.RequiredLength = 6;
            options.Password.RequiredUniqueChars = 1;

            // Lockout settings.
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;
            options.Lockout.AllowedForNewUsers = true;

            // User settings.
            options.User.AllowedUserNameCharacters =
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
            options.User.RequireUniqueEmail = false;
        });

        services.AddAuthentication()
            .AddMicrosoftAccount(options =>
            {
                options.ClientId = Configuration["Authentication:Microsoft:ClientId"];
                options.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
            });

        services.AddAuthorization(options =>
        {
            options.AddPolicy(PolicyNames.RequireTauchbold, policy => policy.RequireRole(Rolenames.Tauchbold));
        });

在 Configure() 中我有:

app.UseAuthentication();

然后在我的控制器中我有:

[Authorize(Policy = PolicyNames.RequireTauchbold)]
public class EventController : Controller
{
    ...

可以在 GitHub 上找到完整的源代码。

问题

问题是即使我正确登录并分配了角色,对控制器的上述检查总是返回“拒绝访问”。我不知道这里会出错。有人知道我会想念什么here吗?

更新

我认为,普通的空 [Authorize] 属性有效(强制登录)但 [Authorize(Policy = '...')] 不识别角色。我检查了数据库表,但它们对我来说看起来不错。除了ÀspNetUsersAspNetRolesAspNetUserRoles 之外,我还需要在数据库中配置其他任何内容吗?

更新 2:

我使用@itminus 的解决方案让它工作,但必须在启动时添加对AddDefaultUI() 的调用才能使登录和注册再次工作。所以我的启动现在包含这些行来配置身份:

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

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc asp.net-core-identity


    【解决方案1】:

    我刚刚测试了您的代码。由于2.1.x版本中的AddDefaultIdentity&lt;IdentityUser&gt;()默认不会启用Role,所以我将您的代码更改如下:

    //services.AddDefaultIdentity<IdentityUser>()
    //    .AddEntityFrameworkStores<ApplicationDbContext>();
    
    services.AddIdentity<IdentityUser, IdentityRole>()
        .AddRoleManager<RoleManager<IdentityRole>>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
    

    为了使用上面的代码进行测试,我注册了一个新用户并为该用户添加了角色:

    await _roleManager.CreateAsync(new IdentityRole(Rolenames.Tauchbold));
    var user= await _userManager.GetUserAsync(HttpContext.User);
    await _userManager.AddToRoleAsync(user, Rolenames.Tauchbold);
    

    先退出再登录,现在可以了:

    【讨论】:

    • 听起来很有希望!你的_roleManger_userManager 是什么类型?您是否注入了它们?如果是,您是如何将它们注册到容器中的?
    • 创建代码可以与RoleManager&lt;&gt;UserManager&lt;&gt; 一起使用,但现在“登录”和“注册”不再执行任何操作。你在这里改变了什么吗?给他们搭建脚手架?
    • 通过在启动中添加对AddDefaultUI() 的调用也可以实现此目的。非常感谢您的这些指点!
    • @Marc 我只是为它们搭建脚手架,并没有手动添加 .AddDefaultUI() 。我这样做是因为我们将来可能会自定义用户和角色,在这种情况下,我们必须在 Razor 页面中自定义 userManagerroleManager。顺便说一句,我个人认为让我的 DbContext 明确地继承自IdentityDbContext&lt;IdentityUser,IdentityRole&gt; 很好,所以我将其设为public class ApplicationDbContext : IdentityDbContext&lt;IdentityUser, IdentityRole,string&gt;
    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 2015-01-09
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多