2010-05-20 52 views
0

我看到所有的地方,但我找不到這樣一個例子(也許我不知道正確的措辭),我試圖建立使用ASP .Net MVC2(但任何例子只是ASP。網絡也是有幫助的)一個過程,在註冊過程結束時會發送一個鏈接給用戶,讓他確認他的註冊。此外,一個類似的過程,讓用戶重置他的密碼,與典型的「忘記密碼」,併發送鏈接/網址,以便用戶可以點擊並鍵入一個新的密碼。有人能幫我找到一個例子,或者至少讓我知道如何「谷歌」呢?如何使用ASP.Net MVC2發送鏈接/網址以確認用戶註冊和/或密碼重置/恢復?

謝謝, 馬克

回答

0

對於忘記密碼,你可以做一個視圖這樣

<% using (Html.BeginForm()) {%> 
    <%: Html.ValidationSummary(true)%> 

<p><%: ViewData["Error"] %></p> 
<p> Have you forgotten you password? No problem!</p> 

<div class="editor-label"> 
    <%: Html.Label("Fill in your username") %> 
</div> 
<div class="editor-field"> 
    <%: Html.TextBox("userName") %> 
</div> 

<p> and <input type="submit" value="click here" /> to reset your password.</p> 

<% } %> 

,並作爲控制器(請先從ASPNETDB的模型(如果您沒有看到它按「顯示所有文件」按鈕))

這必須在控制器定義後立即放置

aspnetdbEntities aspnetdb = new aspnetdbEntities(); 

然後遵循這一

public ActionResult ForgottenPassword() 
    { 
     return View(); 
    } 

[HttpPost] 
public ActionResult ForgottenPassword(FormCollection formValue) 
{ 
    var userName = formValue["userName"]; 
    try 
     { 
      var useraccount = aspnetdb.aspnet_Users.Single(c => c.UserName == userName); 
      var fromAddress = "put an email-address"; 

      var message = new MailMessage(fromAddress, user.Email) 
      { 
       Subject = "", 
       Body = "a link to a controller that lets the user put in a 
         new password and then save that password to the aspnetdb." 
         (that controller will most likley require a username) 
         "or a link with a new random password in it tht you have put 
         as his password like this:" 
         useraccount.aspnet_Membership.Password = "random password"; 
         aspnetdb.SaveChanges; 

      }; 

      var client = new SmtpClient("smtpServerName"); 
      client.Send(message); 

      return View(); 
     } 

     catch 
     { 
      ViewData["Error"] = "Please give up an existing username"; 
      return View(); 


     } 
    } 

我希望這個答案是有幫助的。

相關問題