Identity IUserEmailStore生成密码重置令牌错误

本文关键字:令牌 错误 密码 IUserEmailStore Identity | 更新日期: 2023-09-27 18:13:25

相关问题:Webforms ASP。NET身份系统重置密码

我正在尝试使用身份系统实现密码恢复,但遇到错误(商店不实现IUserEmailStore)。这是我正在做的,我正在使用Visual Studio 2013 Web。使用Web Forms (MVC正在学习中),用户用他们的电子邮件注册,并存储在数据库的username字段中。我在IdentityModel.cs中添加了UserManager类:

public class UserManager : UserManager<ApplicationUser>
{
    public UserManager()
        : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
        this.UserTokenProvider = new EmailTokenProvider<ApplicationUser, string>();
        this.EmailService = new EmailService();
    }
} 
public class EmailService : IIdentityMessageService
{
     public Task SendAsync(IdentityMessage message)
       {
        //email service here to send an email.
        return Task.FromResult(0);
       }
}

在IdentityModels.cs中,我还添加了帮助器:

public static string GetResetPasswordRedirectUrl(string code)
    {
        return "/Account/ResetPassword?" + CodeKey + "=" + HttpUtility.UrlEncode(code);
    }

这些是我在IdentityModels.cs类中所做的所有更改。现在是ForgotPassword。我在aspx页面做了以下操作:

 protected void ResetPassword(object sender, EventArgs e)
    {
        if (IsValid)
        {
             var manager = new UserManager();
             var user = new ApplicationUser();
             user = manager.FindByName(Email.Text);                
            // Check if the the user does not exist                
            if (user == null)
            {
                ErrorText.Text = "User Could not be found.";
                return;
            }
            string token = manager.GeneratePasswordResetToken(user.Id);
            string callbackUrl = IdentityHelper.GetResetPasswordRedirectUrl(token);
            manager.SendEmail(user.Id, "Reset Password", "Please reset your password by clicking <a href='"" + callbackUrl + "'">here</a>.");
            Link.NavigateUrl = callbackUrl;
        }
    }
我的代码卡住了string token = manager.GeneratePasswordResetToken(user.Id);给出这个异常
{"Store does not implement IUserEmailStore<TUser>."}

异常的详细信息:

System.NotSupportedException was unhandled by user code
  HResult=-2146233067
  Message=Store does not implement IUserEmailStore<TUser>.
  Source=Microsoft.AspNet.Identity.Core
  StackTrace:
      at Microsoft.AspNet.Identity.UserManager`2.GetEmailStore()
      at Microsoft.AspNet.Identity.UserManager`2.<GetEmailAsync>d__a3.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.AspNet.Identity.EmailTokenProvider`2.<GetUserModifierAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.AspNet.Identity.TotpSecurityStampBasedTokenProvider`2.<GenerateAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.AspNet.Identity.UserManager`2.<GenerateUserTokenAsync>d__e9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.UserManager`2.<GeneratePasswordResetTokenAsync>d__4f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.AsyncHelper.RunSync[TResult](Func`1 func)
   at Microsoft.AspNet.Identity.UserManagerExtensions.GeneratePasswordResetToken[TUser,TKey](UserManager`2 manager, TKey userId)
   at uCk.Account.ForgotPassword.Forgot(Object sender, EventArgs e) in c:'Users'Tim'Documents'Visual Studio 2013'Projects'uCk'uCk'Account'ForgotPassword.aspx.cs:line 38
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

我从例外中理解的是,我应该实现IUserEmailStore接口?我不知道我应该在这里做什么;如果你看看Usermanager()的实现,我已经添加了EmailService(),难道这还不够吗?我如何克服错误,达到预期的结果?

Identity IUserEmailStore生成密码重置令牌错误

您的UserStore<>实现不实现IUserEmailStore<>,因此您需要从UserStore<>派生以及实现IUserEmailStore<>,如

public class UserStore : UserStore<ApplicationUser>, IUserEmailStore<ApplicationUser>
{
    public UserStore() : base(new ApplicationDbContext()){}
    public Task<TUser> FindByEmailAsync(string email)
    {
        //implement
    }
    //... implement other methods required etc
}

然后在您的管理器构造函数

中引用您的新存储
public class UserManager : UserManager<ApplicationUser>
{
    public UserManager() : base(new UserStore())
    {
        UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
        this.UserTokenProvider = new EmailTokenProvider<ApplicationUser, string>();
        this.EmailService = new EmailService();
    }
}