ASP.NET成员资格电子邮件验证 [英] ASP.NET Membership Email Verification
问题描述
试图基于这篇文章建立在C#中的电子邮件验证。
我创建了一个jangosmtp帐户发送电子邮件。然而,它似乎并不奏效。
Web.config文件:
< system.net>
< mailSettings>
<&SMTP GT;
<网络
主机=relay.example.com端口=25的userName =********密码=********/>
< / SMTP>
< / mailSettings>
< /system.net>
Registration.aspx
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器>
< ASP:CreateUserWizard控件ID =CreateUserWizard1=服务器DisableCreatedUser =真>
< WizardSteps>
< ASP:CreateUserWizardStep ID =CreateUserWizardStep1=服务器/>
< ASP:CompleteWizardStep ID =CompleteWizardStep1=服务器/>
< / WizardSteps>
< MailDefinition BodyFileName =NewAccountTemplate.htm从=example@example.comIsBodyHtml =真主题=步骤来激活您的新帐户...优先=高/>
< / ASP:&的CreateUserWizard GT;
< / ASP:内容>
Registration.aspx.cs:
网页空间
{
公共部分类注册:System.Web.UI.Page
{
保护无效CreateUserWizard1_SendingMail(对象发件人,MailMessageEventArgs E)
{
//发送电子邮件至文件上的地址
用户信息的MembershipUser = Membership.GetUser(CreateUserWizard1.UserName); //构造验证网址
串verifyUrl = Request.Url.GetLeftPart(UriPartial.Authority)+ Page.ResolveUrl(?〜/ Verify.aspx ID =+ userInfo.ProviderUserKey.ToString()); //将<%VerifyUrl%GT;占位符verifyUrl价值
e.Message.Body = e.Message.Body.Replace(&下;%VerifyUrl%gt;中,verifyUrl);
}
}
}
NewAccountTemplate.htm
<!DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/ XHTML1-transitional.dtd>< HTML的xmlns =http://www.w3.org/1999/xhtml>
< HEAD>
<标题>的步骤来激活您的帐号...< /标题>
< /头>
<车身风格=FONT-FAMILY:宋体;> < H2>
欢迎到我的网站<!/ H>
&所述p为H.;
你好,<%USERNAME%取代。您收到此邮件是因为你最近在创建新帐户我
现场。之前您可以登录,但是,你需要先访问以下链接:LT; / P>
&所述p为H.;
&所述; A HREF =&下;%VerifyUrl%gt;中与GT;&下;%VerifyUrl%GT;&下; / A>&下; / P>
&所述p为H.;
访问上面的链接后,您可以登录到该网站<!/ P>
&所述p为H.;
如果您有验证您的帐户有任何问题,请回复此邮件给
获得帮助与LT; / P>
&所述p为H.;
!感谢和LT; / P>< /身体GT;
< / HTML>
Verify.aspx.cs:
网页空间
{
公共部分类验证:页
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
//确保有效的查询字符串值穿过
如果(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 =无效的ID值通过查询字符串传递。
}其他{
// ID存在且洁净,看如果此用户已经被批准
//获取ID查询字符串中发送
GUID用户id =新的GUID(的Request.QueryString [ID]); //获取有关用户的信息
用户信息的MembershipUser = Membership.GetUser(用户ID);
如果(USERINFO == NULL){
//找不到用户!
InformationLabel.Text =的用户帐户无法在会员数据库中找到。
}其他{
//用户是有效的,予以批准
userInfo.IsApproved = TRUE;
Membership.UpdateUser(用户信息); //显示一个消息
InformationLabel.Text =您的帐户已通过验证,现在您可以登录到该网站。
}
}
}
}
}
关于我的这是什么即时通讯假设不导致它的工作有两件事情。
- 它是如何知道甚至派NewAccountTemplate.htm消息?稀释我看到那里更新,现在在createuserwizard1发生。仍然收到此错误消息。
- 在NewAccountTemplate.htm我得到一个警告消息:
警告'<%VerifyUrl%>'没有被发现。
块引用>什么错?我俯瞰的东西。
更新2:
如果我添加onsendingmail =CreateUserWizard1_SendingMail它生成一个链接,但是链接不起作用,因为用户永远不会加入到我选中该数据库。所以,当我点击电子邮件中的链接,它说由于这样的事实不存在与此ID没有用户OBV错误的请求。如果我删除该行$ C $的c中的用户获取创建,但没有联系获取生成:/
解决方案我终于得到了这个工作。
onsendingmail =CreateUserWizard1_SendingMail这应该是在创建用户向导。
< ASP:内容ID =内容2ContentPlaceHolderID =日程地址搜索Maincontent=服务器onsendingmail =CreateUserWizard1_SendingMail>
< ASP:CreateUserWizard控件ID =CreateUserWizard1=服务器DisableCreatedUser =真>
< WizardSteps>
< ASP:CreateUserWizardStep ID =CreateUserWizardStep1=服务器/>
< ASP:CompleteWizardStep ID =CompleteWizardStep1=服务器/>
< / WizardSteps>
< MailDefinition BodyFileName =NewAccountTemplate.htm从=example@example.comIsBodyHtml =真主题=步骤来激活您的新帐户...优先=高/>
< / ASP:&的CreateUserWizard GT;
&刚下使用;%VerificationUrl在NewAccountTemplate.htm%>
更改为registration.aspx.cs
//获取刚刚添加的用户的用户ID
NEWUSER的MembershipUser = Membership.GetUser(CreateUserWizard1.UserName);
GUID newUserId =(GUID)newUser.ProviderUserKey;//确定完整的验证网址(也就是http://yoursite.com/Verification.aspx ID = ...)
字符串urlBase = Request.Url.GetLeftPart(UriPartial.Authority)+ Request.ApplicationPath;
字符串verifyUrl =?Verify.aspx ID =+ newUserId.ToString();
字符串fullUrl = urlBase + verifyUrl;//将<%VerificationUrl%GT;用适当的URL和查询字符串
e.Message.Body = e.Message.Body.Replace(&下;%VerificationUrl%gt;中,fullUrl);
Trying to create an email verification in C# based on this article.
I've created a jangosmtp account to send the email. However it doesn't seem to be working.
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="example@example.com" 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."; } } } } }
Two things concerning me which is what im assuming is not causing it to work.
- How does it know to even send NewAccountTemplate.htm message? UPDATE ahh i see where that happens in the createuserwizard1 now. Still getting this error message.
- On NewAccountTemplate.htm i get a warning message:
Warning '<% VerifyUrl %>' was not found.
What's going wrong? Am i overlooking something.
UPDATE 2:
If i add onsendingmail="CreateUserWizard1_SendingMail" It generate a link, however the link does not work because the user never gets added into the database i checked this. So when i click the link on the email it says bad request obv due to the fact there is no user with this ID. If i remove that line of code the user gets created but no link gets generated :/
解决方案I finally got this to work.
onsendingmail="CreateUserWizard1_SendingMail" This should be in create user wizard.
<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>
use just <%VerificationUrl%> in NewAccountTemplate.htm
Change registration.aspx.cs to
// 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);
这篇关于ASP.NET成员资格电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!