【问题标题】:AspNet Identity MVC6 Violate the type constraintAspNet Identity MVC6 违反类型约束
【发布时间】:2016-10-07 06:22:44
【问题描述】:

我正在尝试自定义 ASP.NET 标识以使用基于整数的键而不是字符串。代码可以编译,但是当我运行应用程序时,它会因违反类型约束而引发错误。以下是我的身份类别的外观:

public class AppUser: IdentityUser<int, AppUserClaim, AppUserRole, AppUserLogin>
{
    public DateTime FirstTrip { get; set; }
}

public class AppRole : IdentityRole<int, AppUserRole, AppRoleClaim>
{
}

public class AppUserClaim : IdentityUserClaim<int>
{
}

public class AppRoleClaim : IdentityRoleClaim<int>
{
}

public class AppUserLogin : IdentityUserLogin<int>
{
}

public class AppUserRole : IdentityUserRole<int>
{
}

public class AppUserToken : IdentityUserToken<int>
{
}

我的 AppDbContext 类:

public class AppDbContext: IdentityDbContext<AppUser,AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
{
    //other code
}

以下是我在 startup.cs 类中设置身份的方式:

services.AddIdentity<AppUser, AppRole>(config =>                    //register ASPNET Identity
        {
            config.User.RequireUniqueEmail = true;
            config.Password.RequiredLength = 8;
            config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";     
        })
        .AddEntityFrameworkStores<AppDbContext, int>();                     

当我运行应用程序时,我收到以下错误:

此设置符合以下建议:

Why does this violate the type constraint?

ASP.NET identity configuration exception and that breaks dotnet.exe run process

我错过了什么?

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc asp.net-identity-2


    【解决方案1】:

    对于遇到类似问题的任何人,这是我必须做的事情:

    1) 实现自定义 UserStoreRoleStore 以说明更改的 TKey。

    2) 在 startup.cs 的 ConfigureServices 方法中注册您的自定义 UserStore 和 RoleStore。因此,而不是建议,

    services.AddIdentity<AppUser, IdentityRole>()
            .AddEntityFrameworkStores<AppDbContext, int>();
    

    使用以下,

    services.AddIdentity<AppUser, IdentityRole>()
            .AddUserStore<AppUserStore<AppDbContext>>()
            .AddRoleStore<AppRoleStore<AppRole, AppDbContext>>();
    

    如果您正在寻找自定义用户和角色存储的实际实现,您可以查看以下链接(在最底部查找 PR1):

    https://github.com/aspnet/Identity/issues/970

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多