ASP身份2-移动令牌的更改到期时间 [英] Asp Identity 2 - Change Expiry Time for Mobile Token
问题描述
我有以下代码可确保电子邮件验证令牌的令牌生存期在14天后过期:-
if (Startup.DataProtectionProvider != null)
{
IDataProtector dataProtector = Startup.DataProtectionProvider.Create("ASP.NET Identity");
this.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser, Guid>(dataProtector)
{
TokenLifespan = TimeSpan.FromDays(14)
};
}
在我应用的其他区域,我通过调用ASP.Identity ApplicationUserManager的GenerateChangePhoneNumberTokenAsync(userId,phoneNumber)方法来使用手机号码令牌.
问题在于,移动令牌将在15分钟后过期.
如何更改移动令牌的有效期?
您需要覆盖
Microsoft.AspNet.Identity.UserManager.GenerateChangePhoneNumberTokenAsync
为此,请查看此处 如何首先扩展UserManager.
在 GenerateChangePhoneNumberTokenAsync 中,您需要使用自定义的 Rfc6238AuthenticationService ,该调用已通过 timeStep 参数调用了 GenerateCode >
GenerateChangePhoneNumberTokenAsync看起来像这样
public class ApplicationUserManager : UserManager<YourIdentityUser, int>
{
public ApplicationUserManager(IUserSecurityStampStore<YourIdentityUser, Guid> store)
: base(store)
{
}
// *** some other code
public override async Task<string> GenerateChangePhoneNumberTokenAsync(Guid userId, string phoneNumber)
{
var user = await FindByIdAsync(userId);
var code = CustomRfc6238AuthenticationService.GenerateCode(user.SecurityStamp, phoneNumber, "optional modifier", TimeSpan.FromDays(14));
return code;
}
}
和自定义 Rfc6238AuthenticationService 的示例实现可以在此处找到>
I have the following code that ensures the Token lifetime span for email verification tokens expire after 14 days :-
if (Startup.DataProtectionProvider != null)
{
IDataProtector dataProtector = Startup.DataProtectionProvider.Create("ASP.NET Identity");
this.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser, Guid>(dataProtector)
{
TokenLifespan = TimeSpan.FromDays(14)
};
}
In a different area of my app, I'm using mobile phone number tokens by calling the GenerateChangePhoneNumberTokenAsync(userId, phoneNumber) method of the ASP.Identity ApplicationUserManager.
The problem is that the mobile tokens are expiring after 15 minutes.
How do i change the lifetime of the mobile tokens?
You need to override
Microsoft.AspNet.Identity.UserManager.GenerateChangePhoneNumberTokenAsync
To do so please have a look here and here how to extend the UserManager first.
In GenerateChangePhoneNumberTokenAsync you need to use a custom Rfc6238AuthenticationService which has call to GenerateCode with timeStep parameter
The GenerateChangePhoneNumberTokenAsync will look like this
public class ApplicationUserManager : UserManager<YourIdentityUser, int>
{
public ApplicationUserManager(IUserSecurityStampStore<YourIdentityUser, Guid> store)
: base(store)
{
}
// *** some other code
public override async Task<string> GenerateChangePhoneNumberTokenAsync(Guid userId, string phoneNumber)
{
var user = await FindByIdAsync(userId);
var code = CustomRfc6238AuthenticationService.GenerateCode(user.SecurityStamp, phoneNumber, "optional modifier", TimeSpan.FromDays(14));
return code;
}
}
and the sample implementation of custom Rfc6238AuthenticationService can be found here
这篇关于ASP身份2-移动令牌的更改到期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!