使用整数用户 ID 扩展 EF Core Identity UserStore

Posted

技术标签:

【中文标题】使用整数用户 ID 扩展 EF Core Identity UserStore【英文标题】:Extending EF Core Identity UserStore with integer user ID 【发布时间】:2021-07-09 08:40:21 【问题描述】:

使用 Blazor 服务器、dotnet 5 和 Entity Framework Core,我已经成功地使用整数 ID 自定义身份

例如

public class ApplicationUser : IdentityUser<int>

    [Required]
    [MaxLength(50)]
    public string FirstName  get; set; 

    [Required]
    [MaxLength(50)]
    public string LastName  get; set; 

我现在想为自定义登录扩展 UserStore 以存储密码历史记录以防止重复。记录在案的方法似乎是:

public class ApplicationUserStore : UserStore<ApplicationUser>

    public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
    
    

    public override async Task<IdentityResult> CreateAsync(ApplicationUser appUser)
    
        await base.CreateAsync(appUser);
        await AddToUsedPasswordAsync(appUser, appUser.PasswordHash);
    

    public Task AddToUsedPasswordAsync(ApplicationUser appuser, string userpassword)  
      
        appuser.UserUsedPassword.Add(new UsedPassword()  UserID = appuser.Id, HashPassword = userpassword );  
        return UpdateAsync(appuser);  
     
 

这适用于默认 GUID Id,但使用整数时会引发错误:

ApplicationUser 类型不能用作泛型类型或方法“UserStore”中的类型参数“TUser”。没有从 ApplicationUser' 到 'Microsoft.AspNetCore.Identity.IdentityUser' 的隐式引用转换。

这样做的正确方法是什么?

编辑:

每@Yinqiu,修改为自定义ApplicationRole到

公共类 ApplicationUserStore : UserStore

    public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
    
    

成功构建并创建用户,但在尝试访问 ApplicationUserManager.IsInRoleAsync 时出现运行时错误:

"无法为 'IdentityUserRole' 创建 DbSet,因为该类型未包含在上下文模型中。"

但是我有一个自定义用户角色:

public class ApplicationUserRole : IdentityUserRole<int>

    public ApplicationUserRole() : base()  

    public bool IsSystemEssential  get; set; 


在 ApplicationDbContext 中有定义:

公共 DbSet ApplicationUserRoles get;放;

【问题讨论】:

嗨@MaxTheCoder,有什么更新吗? 这不是一个论坛,请不要添加问题的解决方案。如果您想表明您找到的答案不是已发布的答案,请发布答案 【参考方案1】:

已解决(暂时)

查看重载继承选项后,您必须进行如下扩展:

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext, int, ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationUserToken, ApplicationRoleClaim>

    public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
    
    

这似乎有效。仍然需要检查所有角色并声明更新。

【讨论】:

【参考方案2】:

您可以按以下方式更改您的ApplicationUserStore

public class ApplicationUserStore : UserStore<ApplicationUser,IdentityRole<int>, ApplicationDbContext,int>

    public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
    
    
    //...

【讨论】:

我还自定义了角色,因此您的建议适用于以下修改:public class ApplicationUserStore : UserStore。但是,然后在调用 IsInRole 时出现以下错误:无法为 'IdentityUserRole' 创建 DbSet,因为此类型未包含在上下文模型中。"

以上是关于使用整数用户 ID 扩展 EF Core Identity UserStore的主要内容,如果未能解决你的问题,请参考以下文章

如何使 EF-Core 使用 Guid 而不是 String 作为其 ID/主键

如何比较 EF Core 插值查询中的整数列表

EF Core:实体类型“用户”需要定义主键

EF Core外键配置问题代码优先问题

对EF Core进行扩展使支持批量操作/复杂查询

具有字段值的 EF Core 结果存在于数组中