AspNet Identity MVC6违反类型约束

时间:2016-10-07 06:22:44

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

我正在尝试自定义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>();                     

当我运行应用程序时,出现以下错误: enter image description here

此设置符合以下建议:

Why does this violate the type constraint?

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

我错过了什么?

1 个答案:

答案 0 :(得分:0)

对于遇到类似问题的人来说,我必须做的就是让它发挥作用:

1)实施自定义 UserStore RoleStore 以考虑已更改的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