【问题标题】:ASP.NET Membership Email VerificationASP.NET 成员资格电子邮件验证
【发布时间】:2012-09-08 19:01:49
【问题描述】:

尝试在 C# 中基于 this article 创建电子邮件验证。

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

Web.config:

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

注册.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="example@example.com" IsBodyHtml="True"  Subject="Steps to activate your new account..." Priority="High" />
    </asp:CreateUserWizard>
</asp:Content>

注册.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>

验证.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" 它会生成一个链接,但是该链接不起作用,因为用户从未被添加到数据库中,我检查了这一点。因此,当我单击电子邮件上的链接时,它会显示错误的请求 obv,因为没有具有此 ID 的用户。如果我删除该行代码,则创建用户但未生成链接:/

【问题讨论】:

  • 假设您只是不小心发布了您的帐户凭据...更改它们! o_O
  • 这只是我为此目的创建的一个免费测试器。反正改了。
  • 关于它为什么不起作用的任何想法?
  • 您的代码引用了 ASP.NET 向导控件,但我看不到您在使用它。
  • 查看帖子,我对其进行了编辑以显示我的registration.aspx代码。

标签: c# asp.net smtp


【解决方案1】:

我终于搞定了。

  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="example@example.com" IsBodyHtml="True"  Subject="Steps to activate your new account..." Priority="High" />
    </asp:CreateUserWizard>
    

  2. 仅在 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);
    

【讨论】:

    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2010-09-30
    • 1970-01-01
    • 2012-07-23
    • 2016-09-09
    相关资源
    最近更新 更多