1

먼저 내가 MVC 아주 새로운 오전과 내가 MVC I는이 기술이 여기에 게시하여 구현하고 4. 암호 복구 기능을 구현하기 위해 노력하고 있습니다 : 그것은 그러나 작동 내가 방법을 이해 Where to find C# sample code to implement password recovery in ASP .NET MVC2MVC 4 비밀 번호 복구

을 지금 구현하려고하는 도우미 클래스가 없습니다. 나는 클래스에 대해 이야기하고있다 : NotificationsHelper.SendPasswordRetrieval (model.Email, this.ControllerContext);

컨트롤러의 RetrievePassword acction 제어기 파라미터 PasswordRetrievalModel 모델있다. 이 클래스는 db에 연결하고 테마 사이에 몇 가지 속성을 구현하는 클래스 모델이라고 가정합니다. 이라는 문자열 속성이 있습니다. 이 올바른지?

이상 NotificationsHelper.SendPasswordRetrieval (model.Email, this.ControllerContext); static 클래스는이 static 메소드 을 구현합니다. SendPasswordRetrievla은 2 개의 paramateres가 있습니다 : model.Email 이는 PasswordRetrievalModel 모델 클래스의 문자열 속성이므로, 우리가 이메일을 보낼 사용자 이메일이됩니다. 두 번째 매개 변수는 this.ControllerContext입니다. 이 매개 변수의 요점은 무엇입니까? SendPasswordRetrieval 메서드로 전송되는 값은 무엇입니까?

이 같은 클래스 구현보다 : 나는 어떻게 제공 한 코드 @agarcian를 사용하여 토큰이 가지고 마십시오 URL 포맷 라인을 열거 위의 코드에서

public static class NotificationsHelper 
{ 
    public static bool SendPasswordRetrieval(string emailAddress, ControllerContext ctx) 
    { 
     try 
     { 
      StringBuilder emailMessage = new StringBuilder(); 

      emailMessage.Append("<br />"); 
      emailMessage.Append("Hello,"); 
      emailMessage.Append("You have requested a password recovery."); 
      emailMessage.Append("<br />"); 
      emailMessage.Append("Please click the link below to change your password: <br />"); 
      emailMessage.Append("<br />"); 
      emailMessage.Append(string.Format("http://www.example.com/Account/Validate?email={0}&token={1}", emailAddress, "**345982374532453435345**")); 
      emailMessage.Append("<br />"); 

      MailMessage email = new MailMessage(); 
      email.From = new MailAddress("[email protected]"); 
      email.To.Add(new MailAddress(emailAddress)); 
      email.Subject = "domain.com Password Recovery"; 
      email.Body = emailMessage.ToString(); 
      email.IsBodyHtml = true; 

      SmtpClient smtpServer = new SmtpClient(); 
      smtpServer.Host = "smtp.gmail.com"; 
      smtpServer.Port = 587; 
      smtpServer.Credentials = new NetworkCredential("username", "password"); 
      smtpServer.EnableSsl = true; 
      smtpServer.Send(email); 
      return true; 
     }    
     catch (Exception e) 
     { 
      Trace.WriteLine(String.Format("Failure to send email to {0}.", emailAddress)); 
      return false; 
     } 
    } 
} 

를? 토큰이 두 번째 매개 변수에서 왔는지 ControllerContext? 그렇다면 어떻게해야합니까? 기존 테이블에 열을 추가하지 않으려면 사용자 요청이 암호가 해당 사용자에 대한 pwdresetTocket 필드에 Guid.NewGuid()를 삽입 다시 콜백 URL

에서 동일한를 추가 할 때

답변

0

것은, usertable 이름을 pwdresetTocket 새로운 열을 추가 , 새 테이블을 작성하여이를 사용자 테이블에 맵핑 할 수 있습니다.

그러면 방법은 다음과 같습니다.

public static bool SendPasswordRetrieval(string emailAddress, ControllerContext ctx) 
    { 
     try 
     { 
      StringBuilder emailMessage = new StringBuilder(); 
      string token = Guid.NewGuid(); 
     // call to a method that will update the table with token 
     updateUsertablewithResetTocket(tocken); 

      emailMessage.Append("<br />"); 
      emailMessage.Append("Hello,"); 
      emailMessage.Append("You have requested a password recovery."); 
      emailMessage.Append("<br />"); 
      emailMessage.Append("Please click the link below to change your password: <br />"); 
      emailMessage.Append("<br />"); 
      emailMessage.Append(string.Format("http://www.example.com/Account/Validate?email={0}&token={1}", emailAddress, token)); 
      emailMessage.Append("<br />"); 

      MailMessage email = new MailMessage(); 
      email.From = new MailAddress("[email protected]"); 
      email.To.Add(new MailAddress(emailAddress)); 
      email.Subject = "domain.com Password Recovery"; 
      email.Body = emailMessage.ToString(); 
      email.IsBodyHtml = true; 

      SmtpClient smtpServer = new SmtpClient(); 
      smtpServer.Host = "smtp.gmail.com"; 
      smtpServer.Port = 587; 
      smtpServer.Credentials = new NetworkCredential("username", "password"); 
      smtpServer.EnableSsl = true; 
      smtpServer.Send(email); 
      return true; 
     }    
     catch (Exception e) 
     { 
      Trace.WriteLine(String.Format("Failure to send email to {0}.", emailAddress)); 
      return false; 
     } 
    } 

사용자가 암호를 다시 설정하면, 리셋 토큰 칸을 비워

관련 문제