Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重置密码时出错_C#_Asp.net_Asp.net Mvc 4_Asp.net Identity_Forgot Password - Fatal编程技术网

C# 重置密码时出错

C# 重置密码时出错,c#,asp.net,asp.net-mvc-4,asp.net-identity,forgot-password,C#,Asp.net,Asp.net Mvc 4,Asp.net Identity,Forgot Password,我创建了MVC4应用程序。在该应用程序中,如果用户忘记了密码,我有方法向用户发送电子邮件以重置密码。我正在使用asp.net身份成员身份 在web服务器中部署此项目时,我收到以下错误消息。它在我的本地主机模式下工作得很好 错误消息 无法编辑此用户,因为数据保护操作不成功。 这可能是由于未加载的用户配置文件导致的 当前线程的用户上下文,当 线程正在模拟 这是忘记密码的方法 [AllowAnonymous] public ActionResult ForgotPassword()

我创建了MVC4应用程序。在该应用程序中,如果用户忘记了密码,我有方法向用户发送电子邮件以重置密码。我正在使用asp.net身份成员身份

在web服务器中部署此项目时,我收到以下错误消息。它在我的本地主机模式下工作得很好

错误消息

无法编辑此用户,因为数据保护操作不成功。 这可能是由于未加载的用户配置文件导致的 当前线程的用户上下文,当 线程正在模拟

这是忘记密码的方法

    [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);
    }
[AllowAnonymous]
public ActionResult ForgotPassword()
{
返回视图();
}            
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
public async Task“、user.UserName、Url.Action(“ResetPassword”、“Account”、new{UserId=user.Id、code=code}、Request.Url.Scheme”)+string.Format(“

; 字符串attachmentPath=Server.MapPath(“~/Images/hec logo.png”); string contentID=Path.GetFileName(attachmentPath)。替换(“.”,“)+“@zofm”; 附件内联=新附件(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、 附件。添加(内联); //用正确的内容ID替换标记 m、 Body=m.Body.Replace(“@@IMAGE@@@cid:+contentID”); System.Net.Mail.SmtpClient smtp=新系统.Net.Mail.SmtpClient(“11.11.11.111”); smtp.Port=11; smtp.Credentials=新系统.Net.NetworkCredential(“sample@email.lk", "8888888"); smtp.EnableSsl=false; smtp.Send(m); //不要透露用户不存在或未确认 } 返回视图(“放弃密码确认”); } 其他的 { AddModelError(“,”用户名或电子邮件ID无效。“); } //如果我们走到这一步,有些东西失败了,重新显示形式 返回视图(模型); }
我也有同样的问题,经过多次研究,我发现问题出在IIS部署中

因此,遵循这个思路,我能够解决我的问题

  • 打开IIS管理器

  • 通过选择应用程序,右键单击应用程序,然后选择管理应用程序->高级,了解应用程序正在使用的应用程序池
    设置

  • 之后,在左上角,选择应用程序池,然后继续选择应用程序使用的应用程序池

  • 右键单击它,选择高级设置,转到流程模型部分,找到“加载用户配置文件”选项并将其设置为


我想知道这是否与?没有标记尽可能重复的问题相同,因为我不确定:)是否存在将“加载用户配置文件”=真的风险?