程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Asp.NET Identity 2给出“无效令牌”错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Asp.NET Identity 2给出“无效令牌”错误?

开发过程中遇到Asp.NET Identity 2给出“无效令牌”错误的问题如何解决?下面主要结合日常开发的经验,给出你关于Asp.NET Identity 2给出“无效令牌”错误的解决方法建议,希望对你解决Asp.NET Identity 2给出“无效令牌”错误有所启发或帮助;

由于您在此处生成用于重置密码的令牌:

String code = UseRMANager.GeneratepasswordresetToken(user.ID);

但是实际上尝试验证电子邮件的令牌:

result = await UseRMANager.ConfirmEmailAsync(ID, codE);

这是2个不同的令牌。

在您的问题中,您说您正在尝试验证电子邮件,但是您的代码用于密码重置。你在做什么

如果您需要电子邮件确认,请通过生成令牌

var emailConfirmationCode = await UseRMANager.GenerateEmailConfirmationTokenAsync(user.ID);

并通过确认

var confirmResult = await UseRMANager.ConfirmEmailAsync(userID, codE);

如果您需要重置密码,请生成以下令牌:

var code = await UseRMANager.GeneratepasswordresetTokenAsync(user.ID);

并确认如下:

var resetResult = await useRMANager.resetpasswordAsync(user.ID, code, newpassword);

解决方法

我正在使用 Asp.Net-Identity-2, 并且尝试使用以下方法来验证电子邮件验证代码。但是我收到 “无效令牌” 错误消息

  • 我的应用程序的用户管理器是这样的

    public class AppUseRMANager : UseRMANager<AppUser>
    

    {
    public AppUseRMANager(IUserStore storE) : base(storE) { }

    public static AppUseRMANager Create(IdentityFactoryOptions<AppUseRMANager> options,IOwinContext context)
    {
        AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
        AppUseRMANager manager = new AppUseRMANager(new UserStore<AppUser>(db));
    
        manager.passwordValidator = new passwordValidator { 
            requiredLength = 6,requireNonLetterOrDigit = false,requireDigit = false,requireLowercase = true,requireUppercase = true
        };
    
        manager.UserValidator = new UserValidator<AppUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = true,requireUniqueEmail = true
        };
    
        var dataProtectionProvider = options.DataProtectionProvider;
    
        //token life span is 3 hours
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider =
               new DataProtectorTokenProvider<AppUser>
                  (dataProtectionProvider.Create("ConfirmationToken"))
               {
                   TokenLifespan = TimeSpan.FromHours(3)
               };
        }
    
        manager.Emailservice = new Emailservice();
    
        return manager;
    } //Create
    

    } //class
    } //namespace

  • 我生成令牌的操作是(即使我在此处检查令牌,也会收到“无效令牌”消息):

    [AllowAnonymous]
    

    [httpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Forgotpassword(String email)
    {
    if (ModelState.IsValid)
    {
    AppUser user = UseRMANager.FindByEmail(email);
    if (user == null || !(UseRMANager.IsEmailConfirmed(user.Id)))
    {
    // Returning without warning anything wrong&Hellip;
    return View(“../Home/Index”);

        } //if
    
        String code = UseRMANager.GeneratepasswordResetToken(user.Id);
        String callBACkUrl = Url.Action("Resetpassword","Admin",new { Id = user.Id,code = httpUtility.UrlEncode(codE) },protocol: request.Url.scheR_479_11845@E);
    
        UseRMANager.SendEmail(user.Id,"Reset password Link","Use the following  link to reset your password: <a href=\"" + callBACkUrl + "\">link</a>");
    
        //This 2 lines I use tho debugger propose. The result is: "Invalid token" (???)
        IdentityResult result;
        result = UseRMANager.ConfirmEmail(user.Id,codE);
    }
    
    // If we got this far,something failed,redisplay form
    return View();
    

    } //Forgotpassword

  • 我检查令牌的操作是(在这里,当我检查结果时,我总是得到“无效令牌”):

    [AllowAnonymous]
    

    public async Task Resetpassword(String id,String codE)
    {

    if (id == null || code == null)
    {
        return View("Error",new String[] { "Invalid params to reset password." });
    }
    
    IdentityResult result;
    
    try
    {
        result = await UseRMANager.ConfirmEmailAsync(id,codE);
    }
    catch (InvalidoperationException ioE)
    {
        // ConfirmEmailAsync throws when the id is not found.
        return View("Error",new String[] { "Error to reset password:<br/><br/><li>" + ioe.message + "</li>" });
    }
    
    if (result.Succeeded)
    {
        AppUser objUser = await UseRMANager.FindByIdAsync(id);
        ResetpasswordModel model = new ResetpasswordModel();
    
        model.Id = objUser.Id;
        model.Name = objUser.UserName;
        model.Email = objUser.Email;
    
        return View(model);
    }
    
    // If we got this far,something failed.
    String strErrorMsg = "";
    foreach(String strError in result.Errors)
    {
        strErrorMsg += "<li>" + strError + "</li>";
    } //foreach
    
    return View("Error",new String[] { strErrorMsg });
    

    } //ForgotpasswordConfirmation

我不知道可能遗漏了什么或出了什么问题&Hellip;

大佬总结

以上是大佬教程为你收集整理的Asp.NET Identity 2给出“无效令牌”错误全部内容,希望文章能够帮你解决Asp.NET Identity 2给出“无效令牌”错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。