Shorter Email Token - Asp Identity

I need to create an emailtoken for authentication. Using the default functionality is not an option as the emailtoken expires rather quick.

Using following code I managed to extend the tokenlifetime, but this generates a very long and complex token. Is it possible to generate a six digits token with an extended lifetime?

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

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

Kind Regards,

Brecht

Upvotes: 1

Views: 531

Answers (1)

Solved! Just implement IUserTokenProvider and you can write your own logic!

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
    }
}

Upvotes: 1

Related Questions