2012-09-08 75 views
3

試圖在C#中基於this article創建電子郵件驗證。ASP.NET會員電子郵件驗證

我創建了一個jangosmtp帳戶來發送電子郵件。但它似乎沒有工作。

的Web.config:

<system.net> 
    <mailSettings> 
     <smtp> 
     <network 
      host="relay.example.com" port="25" userName="********" password="********" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 

Registration.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" DisableCreatedUser="True"> 
     <WizardSteps> 
      <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" /> 
      <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" /> 
     </WizardSteps> 
     <MailDefinition BodyFileName="NewAccountTemplate.htm" From="[email protected]" IsBodyHtml="True" Subject="Steps to activate your new account..." Priority="High" /> 
    </asp:CreateUserWizard> 
</asp:Content> 

Registration.aspx.cs:

namespace WebSite 
{ 
    public partial class Registration : System.Web.UI.Page 
    { 
     protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e) 
     { 
      //Send an email to the address on file 
      MembershipUser userInfo = Membership.GetUser(CreateUserWizard1.UserName); 

      //Construct the verification URL 
      string verifyUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Page.ResolveUrl("~/Verify.aspx?ID=" + userInfo.ProviderUserKey.ToString()); 

      //Replace <%VerifyUrl%> placeholder with verifyUrl value 
      e.Message.Body = e.Message.Body.Replace("<%VerifyUrl%>", verifyUrl); 
     } 
    } 
} 

NewAccountTemplate.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Steps to activate your account...</title> 
</head> 
<body style="font-family:Verdana;"> 

    <h2> 
     Welcome to My Website!</h2> 
    <p> 
     Hello, <%UserName%>. You are receiving this email because you recently created a new account at my 
     site. Before you can login, however, you need to first visit the following link:</p> 
    <p> 
     <a href="<%VerifyUrl%>"><%VerifyUrl%></a></p> 
    <p> 
     After visiting the above link you can log into the site!</p> 
    <p> 
     If you have any problems verifying your account, please reply to this email to 
     get assistance.</p> 
    <p> 
     Thanks!</p> 

</body> 
</html> 

Verify.aspx.cs:關於我這是林假設不會導致它的工作

namespace WebSite 
{ 
    public partial class Verify : Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //Make sure that a valid query string value was passed through 
      if (string.IsNullOrEmpty(Request.QueryString["ID"]) || !Regex.IsMatch(Request.QueryString["ID"], "[0-9a-f]{8}\\-([0-9a-f]{4}\\-){3}[0-9a-f]{12}")) 
      { 
       InformationLabel.Text = "An invalid ID value was passed in through the querystring."; 
      } else { 
       //ID exists and is kosher, see if this user is already approved 
       //Get the ID sent in the querystring 
       Guid userId = new Guid(Request.QueryString["ID"]); 

       //Get information about the user 
       MembershipUser userInfo = Membership.GetUser(userId); 
       if (userInfo == null) { 
        //Could not find user! 
        InformationLabel.Text = "The user account could not be found in the membership database."; 
       } else { 
        //User is valid, approve them 
        userInfo.IsApproved = true; 
        Membership.UpdateUser(userInfo); 

        //Display a message 
        InformationLabel.Text = "Your account has been verified and you can now log into the site."; 
       } 
      } 
     } 
    } 
} 

兩件事情。

  1. 它怎麼知道甚至發送NewAccountTemplate.htm消息?更新啊,我現在看到在createuserwizard1中發生了什麼。仍然收到此錯誤消息。
  2. 在NewAccountTemplate.htm我得到一個警告消息:

警告 '<%VerifyUrl%>' 沒有被發現。

什麼錯?我是否可以俯視

更新2:

如果我添加onsendingmail =「CreateUserWizard1_SendingMail」它生成一個鏈接,但是鏈接不起作用,因爲用戶永遠不會被加入到我檢查這個數據庫。所以當我點擊電子郵件上的鏈接時,由於事實上沒有用戶使用這個ID,所以它說不好的請求obv。如果我刪除了這一行的用戶被創建的代碼,但沒有聯繫獲取生成:/

+0

假設你剛剛不小心發佈您的賬戶憑證......改變它們! o_O –

+0

這只是我爲此創建的一款免費測試儀。無論如何改變。 – user1638362

+0

任何想法,爲什麼它不工作? – user1638362

回答

0

我終於得到了這個工作。

  1. onsendingmail =「CreateUserWizard1_SendingMail」這應該在創建用戶嚮導中。

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" onsendingmail="CreateUserWizard1_SendingMail"> 
    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" DisableCreatedUser="True"> 
        <WizardSteps> 
         <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" /> 
         <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" /> 
        </WizardSteps> 
        <MailDefinition BodyFileName="NewAccountTemplate.htm" From="[email protected]" IsBodyHtml="True" Subject="Steps to activate your new account..." Priority="High" /> 
    </asp:CreateUserWizard> 
    

  2. 使用只是<%VerificationUrl%>在NewAccountTemplate.htm

  3. 變化registration.aspx.cs到

    // Get the UserId of the just-added user 
    MembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName); 
    Guid newUserId = (Guid)newUser.ProviderUserKey; 
    
    // Determine the full verification URL (i.e., http://yoursite.com/Verification.aspx?ID=...) 
    string urlBase = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath; 
    string verifyUrl = "Verify.aspx?ID=" + newUserId.ToString(); 
    string fullUrl = urlBase + verifyUrl; 
    
    // Replace <%VerificationUrl%> with the appropriate URL and querystring 
    e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", fullUrl);