【问题标题】:How to add custom TUser and TRole into services.AddIdentity in ASP.NET Core?如何将自定义 TUser 和 TRole 添加到 ASP.NET Core 中的 services.AddIdentity 中?
【发布时间】:2017-07-11 09:15:25
【问题描述】:

我在下一个代码中有错误

public void ConfigureServices(IServiceCollection services)

    services.AddIdentity<ApsUser, ApsRole>(option =>
        {
            option.Password.RequiredLength = 6;
        })

GenericArguments[0], 'project.Core.Users.ApsUser', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim] '违反了类型参数'TUser'的约束。

我的班级

public class ApsUser : IdentityUser<int, UserClaim, UserRole, UserLogin>, IEntity
{
    public UserState State { get; set; }

    #region IEntityLogFields



    #endregion

}

public class UserRole : IdentityUserRole<int>, IEntity<int>
{
    public int Id { get; set; }

    #region IEntityLogFields



    #endregion

}

如何修复错误?

【问题讨论】:

  • 能否展示一下 ApsRole 和 ApsUser 的实现?
  • 我编辑了第一条消息
  • ApsUser 继承自IdentityUser&lt;int, UserClaim, UserRole, UserLogin&gt;, IEntity 背后的原因是什么? ApsUser : IdentityUser还不够吗?
  • 你能像ApsUser : IdentityUserservices.AddIdentity&lt;ApsUser, IdentityRole&gt;(option =&gt; { option.Password.RequiredLength = 6; }) 一样继承吗?不要仅仅为了测试而使用ApsRole
  • 必须是 IdentityUser

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


【解决方案1】:

这是我临时做的

  1. 用户和角色

    public class ApsUser : IdentityUser&lt;int&gt;, IEntity

    公共类 ApsRole : IdentityRole, IEntityLog

  2. startup.cs

    services.AddIdentity(option => { option.Password.RequiredLength = 6; option.Password.RequireLowercase = false; option.Password.RequireNonAlphanumeric = false; option.Password.RequireUppercase = false; option.Password.RequireNonAlphanumeric = false;

        })
            .AddEntityFrameworkStores<ApContext, int>()
            .AddDefaultTokenProviders();
    

【讨论】:

  • .net core 2.2,我找不到带有两个参数的 .AddEntityFrameworkStores。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多