asp.net-core - 为什么这违反了类型参数 'TUser' 的约束?

标签 asp.net-core asp.net-identity asp.net-identity-3

我正在尝试使用以下内容自定义 asp.net 身份实体:

public class User : IdentityUser<string, UserClaim, UserRole, UserLogin>
public class UserClaim : IdentityUserClaim<string>
public class UserLogin : IdentityUserLogin<string>
public class UserRole : IdentityUserRole<string>
public class UserToken : IdentityUserToken<string>
public class Role : IdentityRole<string, UserRole, RoleClaim>
public class RoleClaim : IdentityRoleClaim<string>

然后我创建了一个如下所示的 DbContext 类

public class AppDbContext : IdentityDbContext<User, Role, string, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>

然后用

配置这些
services
    .AddIdentity<User, Role>()
    .AddEntityFrameworkStores<AppDbContext>()
    .AddDefaultTokenProviders();

但我也尝试过

.AddEntityFrameworkStores<AppDbContext, string>()

我在互联网上阅读了很多博客文章,例如 this例如,但大多数都必须处理键数据类型的更改,例如 intGuid。就我而言,我不会更改 string 中的默认键数据类型。

在所有这些情况下,编译都可以,但运行时会抛出错误

System.TypeLoadException GenericArguments[0],'MyIdentity.Entities.User', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`8[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken]' violates the constraint of type parameter 'TUser'.

at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, int numGenericArgs, ObjectHandleOnStack type) at System.RuntimeTypeHandle.Instantiate(Type[] inst) at System.RuntimeType.MakeGenericType(Type[] instantiation)

如果我添加自定义的 UserStore 类,如 this post 中所述

public class UserStore : UserStore<User, Role, AppDbContext, string, UserClaim, UserRole, UserLogin, UserToken>

出现编译错误,说明

CS0311 The type 'MyIdentity.Entities.Role' cannot be used as type parameter 'TRole' in the generic type or method 'UserStore'. There is no implicit reference conversion from 'MyIdentity.Entities.Role' to 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole>'

我做错了什么?

最佳答案

我遇到了这个问题,我发现了这个:"When you are customizing ASP.NET Core Identity, you should not use AddEntityFrameworkStores anymore." 你应该实现你的:

public class ApplicationRoleManager:  RoleManager<Role>
public class ApplicationRoleStore :  RoleStore<Role, ApplicationDbContext, int, UserRole, RoleClaim>
public class ApplicationSignInManager :  SignInManager<User>
public class ApplicationUserManager : UserManager<User>
public class ApplicationUserStore : UserStore<User, Role, ApplicationDbContext, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>

然后将内置服务重定向到您的自定义服务 (Startup.cs):

services.AddScoped<UserStore<User, Role, ApplicationDbContext, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>, ApplicationUserStore> ();
services.AddScoped<UserManager<User>, ApplicationUserManager>();
services.AddScoped<RoleManager<Role>, ApplicationRoleManager>();
services.AddScoped<SignInManager<User>, ApplicationSignInManager>();
services.AddScoped<RoleStore<Role, ApplicationDbContext, int, UserRole, RoleClaim>, ApplicationRoleStore>();
services.AddScoped<IEmailSender, AuthMessageSender>();
services.AddScoped<ISmsSender, AuthMessageSender>();

然后介绍您的定制服务:

services.AddIdentity<User, Role>(identityOptions =>
{
 // ...
 }).AddUserStore<ApplicationUserStore>()
   .AddUserManager<ApplicationUserManager>()
   .AddRoleStore<ApplicationRoleStore>()
   .AddRoleManager<ApplicationRoleManager>()
   .AddSignInManager<ApplicationSignInManager>()
   // You **cannot** use .AddEntityFrameworkStores() when you customize everything
   //.AddEntityFrameworkStores<ApplicationDbContext, int>()
   .AddDefaultTokenProviders();

关于asp.net-core - 为什么这违反了类型参数 'TUser' 的约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39548668/

相关文章:

c# - 使用配置调用了 AddDbContext,但是上下文类型 'ContextClass' 只声明了一个无参数构造函数?

asp.net - 如何在 ASP.Net Core 2.0 应用程序中使用 Bootstrap?

asp.net-core - 如何在配置文件更新后从 Identity Server 4 刷新声明?

c# - 同一数据库中的两个 IdentityDbContext

asp.net-mvc - Asp.Net core MVC6如何在Identity 3中初始添加角色

c# - 在 ASP.NET Core Identity 中检查具有 Authorize 属性的多个策略之一

具有 FromForm 绑定(bind)到 IFormFile 属性的 C# 集成测试 Controller

asp.net-mvc-5 - ASp.net Identity 2.0 示例 - Controller 构造函数、DB 构造函数和 Owin 中间件的重用

c# - userManager.AddToRoleAsync() - 错误 : role does not exist

c# - ASP.NET Core 模型绑定(bind)行为切换