2023-02-15 326
我正在使用IdentityServer3进行身份验证.所有用户都存储在SQL DB中,因此我还使用Microsoft.AspNet.Identity框架进行实际身份验证,并且出于相同的目的,我创建了自己的ApplicationUserManager类.
ASPNET身份已将IOC功能集成到OWIN中间件中.它注册ApplicationUserManager喜欢:
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
它返回ApplicationUserManager>
的新实例的功能委托
public static ApplicationUserManager
Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
但是,IdentityServer使用其自己的DI框架,( i认为)我们不能使用static Create()使用sidentityServer来使用static Create() rgister ApplicationUserManager,Create()方法也可以接受IdentityFactoryOptions IOwinContext作为参数.
我遵循此 我更改了ApplicationUserManager使用构造器注入的实现
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"));
}
}
}
,然后在以下
下用身份服务器自己的DI框架注册所有服务
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在单独的类库中,startup.cs在Web项目中.类库没有引用Web项目.因此,我重构ApplicationUserManager构造函数
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为
factory.Register(new Registration<IDataProtectionProvider>(resolver => Startup.DataProtectionProvider));
以上所述是小编给大家介绍的如何在IdentityServer DI框架下注册ApplicationUserManager?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/33763.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态