ASP.NET成员身份validation电子邮件

尝试根据本文在C#中创建电子邮件validation。

我创建了一个jangosmtp帐户来发送电子邮件。 但它似乎没有起作用。

Web.config文件:

       

Registration.aspx

          

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 https://stackoverflow.com/questions/12333613/asp-net-membership-email-verification/ placeholder with verifyUrl value e.Message.Body = e.Message.Body.Replace("https://stackoverflow.com/questions/12333613/asp-net-membership-email-verification/", verifyUrl); } } } 

NewAccountTemplate.htm

    Steps to activate your account...   

Welcome to My Website!

Hello, . 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:

<a href="https://stackoverflow.com/questions/12333613/asp-net-membership-email-verification/">https://stackoverflow.com/questions/12333613/asp-net-membership-email-verification/

After visiting the above link you can log into the site!

If you have any problems verifying your account, please reply to this email to get assistance.

Thanks!

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上,我收到一条警告消息:

未找到警告“”。

出了什么问题? 我忽略了什么。

更新2:

如果我添加onsendingmail =“CreateUserWizard1_SendingMail”它生成一个链接,但是链接不起作用,因为用户永远不会被添加到数据库中我检查了这个。 因此,当我点击电子邮件上的链接时,由于没有具有此ID的用户,因此显示了错误请求obv。 如果删除该行代码,则会创建用户,但不会生成链接:/

我终于开始工作了。

  1. onsendingmail =“CreateUserWizard1_SendingMail”这应该在创建用户向导中。

             

  2. 在NewAccountTemplate.htm中仅使用<%VerificationUrl%>

  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 (ie, 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);