【问题标题】:Identity IUserEmailStore error on generating password reset token生成密码重置令牌时出现身份 IUserEmailStore 错误
【发布时间】:2014-07-18 13:09:36
【问题描述】:

一些相关的问题: Webforms ASP.NET Identity system reset password

我正在尝试使用身份系统实现密码恢复,但遇到了错误(Store 没有实现 IUserEmailStore)。这就是我正在做的事情,我正在使用 Visual Studio 2013 Web。使用 Web 表单(正在学习 MVC),用户使用他们的电子邮件进行注册,并存储在数据库的用户名字段中。 我在 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;
        }
    }

我的代码卡住了 字符串令牌 = 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 接口?我不确定我应该在这里做什么;如果您查看我添加了 EmailService() 的 Usermanager() 实现,这还不够吗?如何克服错误并达到预期的结果?

【问题讨论】:

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


    【解决方案1】:

    您的UserStore&lt;&gt; 实现没有实现IUserEmailStore&lt;&gt;,因此您需要从UserStore&lt;&gt; 派生并像这样实现IUserEmailStore&lt;&gt;

    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();
        }
    
    } 
    

    【讨论】:

      猜你喜欢
      • 2020-02-29
      • 2015-02-21
      • 2018-09-30
      • 2015-05-29
      • 2015-01-30
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      相关资源
      最近更新 更多