简体   繁体   English

为什么我的 email 确认令牌无效

[英]Why my email confirmation token is invalid

[HttpGet("confirm/{userId}/{token}")]
public async Task<IActionResult> ConfirmEmail(string userId, string token)
{
    User user = _userService.GetById(userId);
    IdentityResult result = await _userManager.
    ConfirmEmailAsync(user, HttpUtility.UrlDecode(token));
    if (result.Succeeded)
    {
        return Ok("Email confirmed successfully!");
    }
    else
    {
        ViewBag.Message = "Error while confirming your email!";
        return SignOut();
    }
}

So my token is invalid at Identity result Result.所以我的令牌在身份结果结果中无效。 Why is this the case and how can i solve this?为什么会这样,我该如何解决? I think it has to do with that the token is not in the database.我认为这与令牌不在数据库中有关。 I am using asp .net core.我正在使用 asp .net 内核。

Your token maybe expired.您的令牌可能已过期。

You can create an class like this to create a custom token provider and set expiration time您可以像这样创建 class 来创建自定义令牌提供程序并设置过期时间

public class CustomEmailConfirmationTokenProvider<TUser> : DataProtectorTokenProvider<TUser> where TUser : class
{
    public CustomEmailConfirmationTokenProvider(IDataProtectionProvider dataProtectionProvider,
            IOptions<EmailConfirmationTokenProviderOptions> options) 
            : base(dataProtectionProvider, options)
    {
    }
}
public class EmailConfirmationTokenProviderOptions : DataProtectionTokenProviderOptions
{
    public EmailConfirmationTokenProviderOptions()
    {
        Name = "EmailDataProtectorTokenProvider";
        TokenLifespan = TimeSpan.FromDays(1);
    }
}

then register this class after services.AddIdentity method like this然后在services.AddIdentity方法之后注册这个 class 像这样

services.AddIdentity<IdentityUser, IdentityRole>(identityOptions =>
            {
                 //DoSomething
            })
            .AddTokenProvider<CustomEmailConfirmationTokenProvider<IdentityUser>>("EmailDataProtectorTokenProvider");

This token expires after one day此令牌在一天后过期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM