重置密码时出错
Getting an error while going to reset password
我创建了 MVC 4 应用程序。在该应用程序中,如果用户忘记了密码,我可以向用户发送电子邮件以重置密码。我正在使用 asp.net 身份成员
我在 Web 服务器中部署此项目时收到以下错误消息。它在我的本地主机模式下完美运行。
错误信息
Cannot edit this User The data protection operation was unsuccessful.
This may have been caused by not having the user profile loaded for
the current thread's user context, which may be the case when the
thread is impersonating.!
这是忘记密码的方法
[AllowAnonymous]
public ActionResult ForgotPassword()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (model.UserName == null)
{
ModelState.AddModelError("", "Please enter the Username");
}
if (model.Email == null)
{
ModelState.AddModelError("", "Please enter the Email ID");
}
if (model.Email == null & model.UserName == null)
{
ModelState.AddModelError("", "Please enter the Username and Email ID");
}
if(ModelState.IsValid)
{
var username = await UserManager.FindByNameAsync(model.UserName);
var user = await UserManager.FindByEmailAsync(model.Email);
if (user != null && username != null)
{
ApplicationDbContext context = new ApplicationDbContext();
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("MyProject");
UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
new System.Net.Mail.MailAddress("sample@email.lk", "My Application"),
new System.Net.Mail.MailAddress(user.Email));
m.Subject = "Reset your Password";
m.IsBodyHtml = true;
m.Body = string.Format("<img src=\"@@IMAGE@@\" alt=\"\"><BR/><BR/>Hi {0},<BR/><BR/>Please click the below link to reset your password. <BR/><BR/> <a href=\"{1}\" title=\"Reset Password\">Reset Password</a>", user.UserName, Url.Action("ResetPassword", "Account", new { UserId = user.Id, code = code }, Request.Url.Scheme)) + string.Format("<BR/><BR/>Regards,<BR/>We Are <BR/>");
string attachmentPath = Server.MapPath("~/Images/hec-logo.png");
string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
m.Attachments.Add(inline);
// replace the tag with the correct content ID
m.Body = m.Body.Replace("@@IMAGE@@", "cid:" + contentID);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("11.11.11.111");
smtp.Port = 11;
smtp.Credentials = new System.Net.NetworkCredential("sample@email.lk", "8888888");
smtp.EnableSsl = false;
smtp.Send(m);
// Don't reveal that the user does not exist or is not confirmed
}
return View("ForgotPasswordConfirmation");
}
else
{
ModelState.AddModelError("", "The Username or Email ID is invalid.");
}
// If we got this far, something failed, redisplay form
return View(model);
}
我遇到了同样的问题,经过多次研究后我发现问题出在 IIS 部署中
所以跟随这个线程我能够解决我的问题
Open your IIS Manager
Find out what AppPool your application is using by selecting your App, right-click on it, and Select Manage Application -> Advanced
Settings.
After that, on the top left hand side, select Applications Pools, and go ahead and select the App Pool used by your app.
Right-click on it, and select Advanced Settings, Go to the Process Model Section and Find the “Load User Profile” Option and set it to
true.
我创建了 MVC 4 应用程序。在该应用程序中,如果用户忘记了密码,我可以向用户发送电子邮件以重置密码。我正在使用 asp.net 身份成员
我在 Web 服务器中部署此项目时收到以下错误消息。它在我的本地主机模式下完美运行。
错误信息
Cannot edit this User The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating.!
这是忘记密码的方法
[AllowAnonymous]
public ActionResult ForgotPassword()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (model.UserName == null)
{
ModelState.AddModelError("", "Please enter the Username");
}
if (model.Email == null)
{
ModelState.AddModelError("", "Please enter the Email ID");
}
if (model.Email == null & model.UserName == null)
{
ModelState.AddModelError("", "Please enter the Username and Email ID");
}
if(ModelState.IsValid)
{
var username = await UserManager.FindByNameAsync(model.UserName);
var user = await UserManager.FindByEmailAsync(model.Email);
if (user != null && username != null)
{
ApplicationDbContext context = new ApplicationDbContext();
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("MyProject");
UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage(
new System.Net.Mail.MailAddress("sample@email.lk", "My Application"),
new System.Net.Mail.MailAddress(user.Email));
m.Subject = "Reset your Password";
m.IsBodyHtml = true;
m.Body = string.Format("<img src=\"@@IMAGE@@\" alt=\"\"><BR/><BR/>Hi {0},<BR/><BR/>Please click the below link to reset your password. <BR/><BR/> <a href=\"{1}\" title=\"Reset Password\">Reset Password</a>", user.UserName, Url.Action("ResetPassword", "Account", new { UserId = user.Id, code = code }, Request.Url.Scheme)) + string.Format("<BR/><BR/>Regards,<BR/>We Are <BR/>");
string attachmentPath = Server.MapPath("~/Images/hec-logo.png");
string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/png";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
m.Attachments.Add(inline);
// replace the tag with the correct content ID
m.Body = m.Body.Replace("@@IMAGE@@", "cid:" + contentID);
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("11.11.11.111");
smtp.Port = 11;
smtp.Credentials = new System.Net.NetworkCredential("sample@email.lk", "8888888");
smtp.EnableSsl = false;
smtp.Send(m);
// Don't reveal that the user does not exist or is not confirmed
}
return View("ForgotPasswordConfirmation");
}
else
{
ModelState.AddModelError("", "The Username or Email ID is invalid.");
}
// If we got this far, something failed, redisplay form
return View(model);
}
我遇到了同样的问题,经过多次研究后我发现问题出在 IIS 部署中
所以跟随这个线程我能够解决我的问题
Open your IIS Manager
Find out what AppPool your application is using by selecting your App, right-click on it, and Select Manage Application -> Advanced
Settings.
After that, on the top left hand side, select Applications Pools, and go ahead and select the App Pool used by your app.
Right-click on it, and select Advanced Settings, Go to the Process Model Section and Find the “Load User Profile” Option and set it to
true.