问题描述:ASP .Net身份验证确认电子邮件不起作用。
解决方法:
protected void RegisterButton_Click(object sender, EventArgs e)
{
// 其他注册逻辑
// 发送确认邮件
MembershipUser user = Membership.GetUser(RegisterUser.UserName);
string confirmationGuid = user.ProviderUserKey.ToString();
string confirmUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/ConfirmEmail.aspx?code=" + confirmationGuid;
string emailBody = "请点击以下链接确认您的电子邮件地址:" + "" + confirmUrl + "";
SendConfirmationEmail(user.Email, emailBody);
}
private void SendConfirmationEmail(string email, string body)
{
// 配置电子邮件发送
SmtpClient smtpClient = new SmtpClient("your_smtp_server");
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
// 创建邮件消息
MailMessage mailMessage = new MailMessage("from_email", email);
mailMessage.Subject = "确认您的电子邮件地址";
mailMessage.Body = body;
mailMessage.IsBodyHtml = true;
// 发送邮件
smtpClient.Send(mailMessage);
}
protected void Page_Load(object sender, EventArgs e)
{
string confirmationCode = Request.QueryString["code"];
if (!string.IsNullOrEmpty(confirmationCode))
{
MembershipUser user = Membership.GetUser(new Guid(confirmationCode));
if (user != null)
{
user.IsApproved = true;
Membership.UpdateUser(user);
ConfirmationLabel.Text = "您的电子邮件地址已确认成功!";
}
else
{
ConfirmationLabel.Text = "确认链接无效!";
}
}
else
{
ConfirmationLabel.Text = "确认链接无效!";
}
}
以上是一个简单的解决方案,根据具体情况可能还需要处理一些其他逻辑,比如错误处理、重发确认邮件等。