在 .Net Core 中设置 UserTokenProvider
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 Core Owin 中不再使用。
现在请告诉我如何初始化
UserTokenProvider
在 .net Core 中不使用 Owin?
您必须使用 TokenOptions.ProviderMap Property 并使用您自己的 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);
});
检查 this thread 了解更多信息:
但是 someone also commented 添加 .AddDefaultTokenProviders() 也可能会解决问题,因此您可以先尝试一下。
services.AddIdentity<User, UserRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
我正在将现有的 Mvc 应用程序转换为 .Net Core,问题是初始化
DataProtectorTokenProvider
在 Mvc 应用程序中它属于
Microsoft.Owin.Security.DataProtection.IDataProtector
以下代码用于在Mvc
中生成UserTokenProviderpublic 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
现在请告诉我如何初始化
UserTokenProvider
在 .net Core 中不使用 Owin?
您必须使用 TokenOptions.ProviderMap Property 并使用您自己的 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);
});
检查 this thread 了解更多信息:
但是 someone also commented 添加 .AddDefaultTokenProviders() 也可能会解决问题,因此您可以先尝试一下。
services.AddIdentity<User, UserRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();