微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – ASP.NET成员身份验证电子邮件

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

我创建了一个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" @R_404_6422@ableCreatedUser="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}"))
            {
                @R_702_404[email protected] = "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 @R_702_4045@ion about the user
                MembershipUser userInfo = Membership.GetUser(userId);
                if (userInfo == null) {
                    //Could not find user!
                    @R_702_404[email protected] = "The user account Could not be found in the membership database.";
                } else {
                    //User is valid,approve them
                    userInfo.IsApproved = true;
                    Membership.UpdateUser(userInfo);

                    //@R_404_6422@play a message
                    @R_702_404[email protected] = "Your account has been verified and you can Now log into the site.";
                }
            }
        }
    }
}

关于我的两件事,即我假设不会导致它起作用.

>如何知道甚至发送NewAccountTemplate.htm消息?更新啊,我现在看到createuserwizard1中发生了什么.仍然收到此错误消息.
>在NewAccountTemplate.htm上,我收到一条警告消息:

Warning ‘<% VerifyUrl %>’ was not found.

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

更新2:

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

解决方法

我终于开始工作了.

> onsendingmail =“createuserWizard1_SendingMail”这应该在创建用户向导中.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server" onsendingmail="createuserWizard1_SendingMail">
<asp:createuserWizard ID="createuserWizard1" runat="server" @R_404_6422@ableCreatedUser="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>

>仅使用<%VerificationUrl%>在NewAccountTemplate.htm中
>将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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐