更短的电子邮件令牌 - Asp身份

时间:2016-05-25 14:09:45

标签: c# asp.net asp.net-identity

我需要创建一个用于身份验证的emailtoken。使用默认功能不是一个选项,因为emailtoken过期很快。

使用以下代码我设法扩展了tokenlifetime,但这会生成一个非常长且复杂的令牌。是否可以生成具有延长寿命的六位数令牌?

var dataProtector = dataProtectionProvider.DataProtectionProvider.Create("My Asp.Net Identity");
userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, string>(dataProtector)
{
    TokenLifespan = TimeSpan.FromHours(24)
};

来源: Email Token Expiring After 15 mins - Asp Identity 2.0 API

亲切的问候,

布莱希特

1 个答案:

答案 0 :(得分:1)

解决!只需实现IUserTokenProvider,您就可以编写自己的逻辑了!

public class CustomUserTokenProvider : IUserTokenProvider<ApplicationUser, string>
{
    public Task<string> GenerateAsync(string purpose, UserManager<ApplicationUser, string> manager,
        ApplicationUser user)
    {
        return
            //logic to generate code
    }

    public Task<bool> IsValidProviderForUserAsync(UserManager<ApplicationUser, string> manager, ApplicationUser user)
    {
        throw new NotImplementedException();
    }

    public Task NotifyAsync(string token, UserManager<ApplicationUser, string> manager, ApplicationUser user)
    {
        throw new NotImplementedException();
    }

    public Task<bool> ValidateAsync(string purpose, string token, UserManager<ApplicationUser, string> manager,
        ApplicationUser user)
    {
        //logic to validate code
    }
}