2015-06-05 29 views
5

Tôi đã tạo ứng dụng MVC 4. Trong ứng dụng đó Nếu người dùng quên mật khẩu, tôi có phương thức gửi email cho người dùng để đặt lại mật khẩu. Tôi đang sử dụng tư cách thành viên asp.netGặp lỗi khi đặt lại mật khẩu

Tôi nhận được thông báo lỗi sau khi triển khai dự án này trong máy chủ web. Nó hoạt động hoàn hảo trong chế độ localhost của tôi.

Thông báo lỗi

không thể chỉnh sửa tài này Các hoạt động bảo vệ dữ liệu không thành công. Điều này có thể do không tải hồ sơ người dùng cho ngữ cảnh người dùng của luồng hiện tại, có thể là trường hợp khi chuỗi đang mạo danh.!

này là phương pháp quên mật khẩu

[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("[email protected]", "My Application"), 
       new System.Net.Mail.MailAddress(user.Email)); 
       m.Subject = "Reset your Password"; 
       m.IsBodyHtml = true; 

       m.Body = string.Format("<img src=\"@@[email protected]@\" 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("@@[email protected]@", "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("[email protected]", "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); 
    } 
+0

Tôi tự hỏi là nó cùng một vấn đề như [này] (http://stackoverflow.com/questions/23455579/generating-reset-password-token-does-not-work -in-azure-website)? Havent 'bị gắn cờ là có thể trùng lặp như tôi không biết chắc chắn :) –

Trả lời

8

tôi đã cùng một vấn đề, sau đó sau nhiều nghiên cứu, tôi phát hiện ra vấn đề đó là trong triển khai IIS

để sau chủ đề này tôi có thể sửa chữa của tôi vấn đề

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.

  • Mở của bạn IIS Manager

  • Tìm hiểu những gì AppPool ứng dụng của bạn đang sử dụng bằng cách chọn App của bạn, nhấp chuột phải vào nó và chọn Manage Application -> Advanced
    Settings.

enter image description here

  • Sau đó, ở phía bên tay trái trên cùng, chọn Applications Pools, và đi trước và chọn App bơi được sử dụng bởi ứng dụng của bạn.

  • Nhấp chuột phải vào nó và chọn Advanced Settings, Tới Mục Process Model và Tìm “Load Hồ sơ người dùng” Lựa chọn và đặt nó vào
    đúng.

enter image description here

+0

có bất kỳ rủi ro nào để đặt "Tải hồ sơ người dùng" = true không? – Mahdi

Các vấn đề liên quan