如何在IdentityServer DI框架中注册ApplicationUserManager? [英] How to register ApplicationUserManager with IdentityServer DI framework?
问题描述
我正在使用IdentityServer3进行身份验证.所有用户都存储在Sql DB中,因此我也使用Microsoft.AspNet.Identity
框架进行实际身份验证,并且出于相同的目的,我创建了自己的ApplicationUserManager
类.
I am using IdentityServer3 for authentication. All users are stored in Sql DB so I am also using Microsoft.AspNet.Identity
framework for actual authentication, and for the same purpose i have created my own ApplicationUserManager
class.
AspNet身份具有集成到OWIN中间件中的IoC功能.并按如下方式注册ApplicationUserManager
:
The AspNet identity has IoC feature integrated into OWIN middleware. and it registers the ApplicationUserManager
like:
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
使用函数委托,该函数返回ApplicationUserManager
it takes function delegate which returns a new instance of a ApplicationUserManager
public static ApplicationUserManager
Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
但是,IdentityServer使用其自己的DI框架,并且(我认为)我们不能使用静态Create()
方法对IdentityServer进行ApplicationUserManager
的设置,而且Create()
方法也需要IdentityFactoryOptions
和一个IOwinContext
作为参数.
However, IdentityServer uses its own DI framework and (i think) we cannot use static Create()
method to rgister ApplicationUserManager
with IdentityServer, Also Create()
method takes IdentityFactoryOptions
and a IOwinContext
as parameters.
我关注了这个 SO帖子,我将ApplicationUserManager
的实现更改为使用构造函数注入
I followed this SO post and i change the implementation of ApplicationUserManager
to use Constructor Injection
public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
public ApplicationUserManager(ApplicationUserStore store, IdentityFactoryOptions<ApplicationUserManager> options)
: base(store)
{
// Configure validation logic for usernames
UserValidator = new UserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
UserLockoutEnabledByDefault = true;
DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
MaxFailedAccessAttemptsBeforeLockout = 5;
EmailService = new EmailService();
SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
}
}
然后使用下面的IdentityServer自己的DI框架注册所有服务
And then register all the services with IdentityServer's own DI framework as below
factory.UserService = new Registration<IUserService, UserService>();
factory.Register(new Registration<ApplicationUserManager>());
factory.Register(new Registration<ApplicationUserStore>());
factory.Register(new Registration<IdentityFactoryOptions<ApplicationUserManager>>(resolver => new IdentityFactoryOptions<ApplicationUserManager>
{
DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ASP.NET Identity")
}));
factory.Register(new Registration<ApplicationDbContext>(resolver => new ApplicationDbContext(ApplicationConfig.ConnectionString)));
问题
- 这是向
ApplicationUserManager
注册的正确方法吗 IdentiServer的DI框架? - 我在注册过程中创建了
DataProtectionProvider
,并在构造函数中创建了UserTokenProvider
. IdentityServer如何使用这两个提供程序? - 请注意,自从 ApplicationUserManager的构造函数不再需要它,它将 导致OWIN管道出现任何问题?
- 什么是
ApplicationUserManager
的理想注册模式?
- Is this a correct way of registering
ApplicationUserManager
with IdentiServer's DI framework? - I am creating
DataProtectionProvider
during the registration and theUserTokenProvider
inside the constructor. How this 2 providers are being used by IdentityServer? - Note that im not registering
IOwinContext
anywhere since ApplicationUserManager's constructor does not need it anymore, will that cause any issue down the OWIN pipeline?. - What would be ideal registration mode for
ApplicationUserManager
?
推荐答案
这两篇文章有助于解决我的问题
These two articles helped to solve my issue
http://tech.trailmax. info/2014/09/aspnet-identity-and-ioc-container-registration/
但是我的ApplicationUserManager
在单独的类库中,而startup.cs
在Web项目中.类库没有对该Web项目的引用.所以我重构了ApplicationUserManager
构造器
However my ApplicationUserManager
is in separate class library and startup.cs
is in web project. The class library does not have reference to the web project. So i refactored ApplicationUserManager
Constructor
public ApplicationUserManager(ApplicationUserStore store, IDataProtectionProvider dataProtectionProvider)
: base(store)
{
// other stuff
if (dataProtectionProvider != null)
{
UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("UserToken"));
}
}
,并且还在DI框架中注册了IDataProtectionProvider
.我没有将Unity用作IoC.我正在使用IdentityServer自己的DI框架.所以我将IDataProtectionProvider
注册为
and also registered IDataProtectionProvider
with DI framework. I am not using Unity as IoC. I am using IdentityServer's own DI framework. So i register IDataProtectionProvider
as
factory.Register(new Registration<IDataProtectionProvider>(resolver => Startup.DataProtectionProvider));
这篇关于如何在IdentityServer DI框架中注册ApplicationUserManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!