简体   繁体   中英

Identity IUserEmailStore error on generating password reset token

Questions of some relevancy: Webforms ASP.NET Identity system reset password

I am trying to implement password recovery using the identity system, but got stuck on the error (Store does not implement IUserEmailStore). Here is what I am doing, I am using Visual Studio 2013 Web. Using Web Forms (MVC learning in progress), the users sign up with their emails, and are stored in the username field in the database. I have added UserManager Class in the IdentityModel.cs as:

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

In IdentityModels.cs I've also added the helper:

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

Those are all the changes I made in the IdentityModels.cs Class. Now for the ForgotPassword.aspx page I have done the following:

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

My code gets stuck on string token = manager.GeneratePasswordResetToken(user.Id); giving this exception

{"Store does not implement IUserEmailStore<TUser>."}

A detailed information on the exception:

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: 

What I understood from the exception is that I should implement the IUserEmailStore Interface? I am not sure what I am supposed to do here; if you look at the Usermanager() implementation I have added EmailService() shouldn't that be enough? How do I overcome the error and achieve the result expected?

your UserStore<> implementation does not implement IUserEmailStore<> so you need to derive from UserStore<> as well as implement IUserEmailStore<> like so

public class UserStore : UserStore<ApplicationUser>, IUserEmailStore<ApplicationUser>
{
    public UserStore() : base(new ApplicationDbContext()){}

    public Task<TUser> FindByEmailAsync(string email)
    {
        //implement
    }

    //... implement other methods required etc
}

then reference your new store in your manager constructor

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

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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