[英]Set UserTokenProvider In .Net Core
我正在将现有的 Mvc 应用程序转换为.Net Core,问题是初始化
DataProtectorTokenProvider
在 Mvc 应用程序中它属于
Microsoft.Owin.Security.DataProtection.IDataProtector
下面的代码用于在 Mvc 中生成UserTokenProvider
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new MySqlUserStore<ApplicationUser>());
//var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
// RequireUniqueEmail = false
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 2,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(50);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
// You can write your own provider and plug it in here.
manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
{
Subject = "Security Code",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new EmailService();
manager.SmsService = new SmsService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
这会自动初始化DataProtectorTokenProvider
现在请告诉我如何初始化
用户令牌提供者
在 .net 内核中不使用 Owin?
您必须使用TokenOptions.ProviderMap 属性并使用您自己的 TokenProvider 并像这样注册它。
services.AddIdentity<User, Role>(options => {
options.Tokens.ProviderMap.Add("ASP.NET Identity", new TokenProviderDescriptor(typeof(MyTokenProvider)));
})
.AddTokenProvider<MyTokenProvider>(nameof(MyTokenProvider));
以及 ConfigureServices 中的可选配置:
services.Configure<DataProtectionTokenProviderOptions>(o =>
{
o.Name = "ASP.NET Identity";
o.TokenLifespan = TimeSpan.FromHours(1);
});
检查此线程以获取更多信息:
但是, 也有人评论说添加.AddDefaultTokenProviders()也可能会解决问题,因此您可以先尝试一下。
services.AddIdentity<User, UserRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
这个答案能回答你的问题吗?
它指出,要配置您的 UserTokenProvider,您可以在ConfigureServices(IServiceCollection services)
方法的StartUp
class 中添加此代码:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.