ASP.NET Core标识-UserManager和UserStore出现问题 [英] ASP.NET Core Identity - UserManager and UserStore woes

查看:80
本文介绍了ASP.NET Core标识-UserManager和UserStore出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ASP.NET Core 应用程序( RC2 库)中实现身份系统,并且有一个特别的困扰使我发疯./p>

首先,我使用EntityFramework.我什至没有使用SQL.我要备份到 RavenDB ,因此我需要实现非常具体.没问题.

所以我设计了一个 RavenUserStore 类,它看起来像这样;

 公共类RavenUserStore< TUser>:IUserStore< TUser> ;,IUserLoginStore< TUser> ;,IUserPasswordStore< TUser> ;,IUserRoleStore< TUser> ;,IUserSecurityStampStore< TUser> ;,IUserClaimStore< TUser> ;,IUserLockoutStore< TUser> ;,IUserTwoFactorStore< TUser> ;,IUserEmailStore< TUser>{//...} 

出色地发挥自己的作用.我已经实现了所有方法,等等.这太好了.非常干净和高效.

现在,我转到我的Web应用程序并进行连接;

  services.AddTransient< ILookupNormalizer>(s =>新的LowerInvariantLookupNormalizer());services.AddTransient< IPasswordHasher< Member>(s => new PasswordHasher< Member>());services.AddTransient< IUserStore< Member>,RavenUserStore< Member>>();services.AddIdentity<成员,角色>(o => {o.Password.RequiredLength = 6;o.Password.RequireDigit = true;o.Password.RequireLowercase = false;o.Password.RequireUppercase = false;}).AddUserStore< RavenUserStore< Member>>().AddRoleStore< RavenRoleStore< Role>();; 

因此,根据我所看到的所有示例以及解决方案

在调用 services.AddIdentity()之前,您需要添加自己的自定义提供程序 .在内部, AddIdentity 使用 TryAddScoped()仅在服务容器中不存在默认项的情况下添加默认项.

因此,只需在注册所有自定义实现后对 AddIdentity()进行调用,就应该意味着它们将具有您期望的优先级.

I'm trying to implement the Identity system in an ASP.NET Core app (RC2 libraries) and there is a particular hangup that is driving me crazy.

First of all, I am not using EntityFramework. I'm not even using SQL. I'm backing up to RavenDB, so I need the implementation to be very specific to that; Which isn't a problem.

So I designed a RavenUserStore class, and it looks like this;

public class RavenUserStore<TUser> :
        IUserStore<TUser>,
        IUserLoginStore<TUser>,
        IUserPasswordStore<TUser>,
        IUserRoleStore<TUser>,
        IUserSecurityStampStore<TUser>,
        IUserClaimStore<TUser>,
        IUserLockoutStore<TUser>,
        IUserTwoFactorStore<TUser>,
        IUserEmailStore<TUser> {
    // ...
}

Works great on its own. I've implemented all the methods, etc. It's wonderful. Very clean and efficient.

Now, I go over to my web application and wire things up;

services.AddTransient<ILookupNormalizer>(s => new LowerInvariantLookupNormalizer());
services.AddTransient<IPasswordHasher<Member>>(s => new PasswordHasher<Member>());
services.AddTransient<IUserStore<Member>, RavenUserStore<Member>>();
services.AddIdentity<Member, Role>(o => {
    o.Password.RequiredLength = 6;
    o.Password.RequireDigit = true;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
})
.AddUserStore<RavenUserStore<Member>>()
.AddRoleStore<RavenRoleStore<Role>>();

So I go make a controller to use this, per all the samples I've seen, and the very core sample from the Identity Framework Github Repository

//... [PROPERTIES]...//
public AccountController(UserManager<Member> userManager, SignInManager<Member> signInManager) {
    // ... [attach constructor parameters to properties] ...//
}

Alright, so I inspect the classes carefully.

UserManager<T> has a property Store,which is a type of IUserStore<T>.

So theoretically.. if the dependency injection resolves types of IUserStore<T> to RavenUserStore<T> when they are injected through a constructor.. shouldn't that mean that the UserManager<T> gets a RavenUserStore<T> as its Store property?

I thought it would too; But when I call methods on the UserManager, it DOES NOT call the ones on my RavenUserStore. Why is this? What can I do?

Do I really have to ALSO make a custom UserManager class and do all of those methods AGAIN?

解决方案

You need to add your own custom providers before calling services.AddIdentity(). Internally, AddIdentity uses TryAddScoped() which only adds the default items if they don't already exist in the services container.

So just putting the call to AddIdentity() after you registered all your custom implementations should mean that they will take precedence as you expect.

这篇关于ASP.NET Core标识-UserManager和UserStore出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆