【问题标题】:asp.net identity custom provider - No authentication handlerasp.net 身份自定义提供程序 - 无身份验证处理程序
【发布时间】:2016-06-20 12:35:10
【问题描述】:

我构建了一个自定义提供程序来支持组和 nosql 数据库。除了身份验证之外,这一切似乎都有效(可以毫无问题地创建帐户)。 我使用了我自己的实体类,这些实体类不是从 identityuser 继承的等应该注意。

我遇到的问题是,例如运行 var loginres = await this.signInManager.PasswordSignInAsync("username", "paswd", false, false) 给了我一个例外,

InvalidOperationException:未配置身份验证处理程序 处理方案:Identity.Application

我设置代码的方式如下:

services.AddIdentity<User, Role>()
                .AddUserStore<NoSqlUserStore<User, Role>>()
                .AddRoleStore<NoSqlRoleStore<Role>>()
                .AddUserManager<IdentityUserManager<User>>()
                .AddRoleManager<IdentityRoleManager<Role>>()
                .AddDefaultTokenProviders();

经理类只是继承了用户和角色管理类,没有任何改变或添加atm。

public class IdentityUserManager<TUser> : UserManager<TUser>
        where TUser : User, new()
    {
        public IdentityUserManager(IUserStore<TUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<TUser> passwordHasher, IEnumerable<IUserValidator<TUser>> userValidators, IEnumerable<IPasswordValidator<TUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<IdentityUserManager<TUser>> logger)
            : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
        {
        }
    }

public class IdentityRoleManager<TRole> : RoleManager<TRole>
        where TRole : Role
    {
        public IdentityRoleManager(IRoleStore<TRole> store, IEnumerable<IRoleValidator<TRole>> roleValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, ILogger<RoleManager<TRole>> logger, IHttpContextAccessor contextAccessor)
            : base(store, roleValidators, keyNormalizer, errors, logger, contextAccessor)
        {
        }
    }

各店铺签名如下:

public class NoSqlRoleStore<TRole> : RoleStoreBase, IRoleStore<TRole>
        where TRole : Role, new()
    {

    }

public class NoSqlUserStore<TUser, TRole> : UserStoreBase<TUser>, //todo: include groups in rolechecking
        IUserLoginStore<TUser>,
        IUserRoleStore<TUser>,
        IUserClaimStore<TUser>,
        IUserPasswordStore<TUser>,
        IUserSecurityStampStore<TUser>,
        IUserEmailStore<TUser>,
        IUserLockoutStore<TUser>,
        IQueryableUserStore<TUser>,
        IUserTwoFactorStore<TUser>,
        IUserAuthenticationTokenStore<TUser>
        where TUser : User, IEntity, new() where TRole : Role, IEntity, new()
    {

    }

当然,它们包含所有接口方法,除了错误之外,它可以编译、运行和工作。 User 和 role 类是简单的实体,有点类似于提供的实体。

我的猜测是我缺少一些配置设置(即使,据我所知,该身份应该提供与此相关的工作默认值?)或其他东西。我从未在默认实体框架提供者之外使用过身份。但我希望有人可能知道这里需要什么? (如果需要更多代码来说明情况,请修复它)

【问题讨论】:

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


    【解决方案1】:

    你应该定义你的自定义 UserStore

    public class MyUserStore : IUserStore<CustomUser,int>,
        IUserPasswordStore<CustomUser,int>, 
        IUserSecurityStampStore<CustomUser, int>,
        IUserLoginStore<CustomUser, int>,IUserRoleStore<CustomUser,int>
            {
                //interfaces implementations
            }
    

    CustomUser 是实现 TUser 接口的 User 类,'int' 是你的主键字段的类型

    如果你有字符串主键使用那个

    IUserStore<CustomUser>
    

    如果你需要定义CustomUserManager

      public class CustomUserManager: UserManager<CustomeUser,int> // UserManager<CustomeUser> if primary is string
        {
    
        }
    

    【讨论】:

    • 我现在使用默认的字符串。但我可以尝试替换泛型并显式设置字符串会有所帮助(看起来很奇怪,它会有所作为,但值得尝试)。
    • 对于默认字符串,只需使用您的 CustomUser 类实现接口
    • 这不是泛型造成的,但我遗漏了一个关键的配置部分,我在检查错误时完全错过了。所以这是我的人为错误。我完全错过了配置方法中的 UseIdentity() 。但是要感谢您,因为您建议的调整使我意识到了这一点(在搜索错误时,最明显的情况很容易失明;))。所以我不赞成:)
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2014-09-28
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多