某些相关性的问题: 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;
}
}
我的代码被卡住了 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()应该不够吗?如何克服错误并达到预期的结果?
答案 0 :(得分:1)
您的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();
}
}