今天我试图关注 Shai Raiten's Blog的这篇文章,当我完成它时,createStatus返回invalidAnswer 这是我的注册行动
[HttpPost]
[AllowAnonymous]
[CaptchaValidation("CaptchaCode", "registerCaptcha", "Wrong captcha!")]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, false, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
MailHelper.SendConfirmationEmail(model.UserName);
return RedirectToAction("Confirmation", "User");
}
else
{
ModelState.AddModelError("", "Failed!");
}
}
return View(model);
}
这是我的RegisterModel.cs
public class RegisterModel
{
[Key]
public long ID { set; get; }
[Required(ErrorMessage = "Do not Skip this")]
public string UserName { set; get; }
[StringLength(500, MinimumLength = 6, ErrorMessage = "Atleast 6 characters in passwords")]
[Required(ErrorMessage = "Do not Skip this")]
public string Password { set; get; }
[Compare("Password", ErrorMessage = "Wrong confirm passwords")]
[Required(ErrorMessage = "Do not skip this")]
public string ConfirmPassword { set; get; }
public string Name { set; get; }
public string Address { set; get; }
[RegularExpression(@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", ErrorMessage = "This is not an email")]
public string Email { set; get; }
public string Phone { set; get; }
public bool EmailConfirm { set; get; }
}
对我的任何建议,非常感谢你们所做的一切帮助。
答案 0 :(得分:6)
你能做的最简单的事情是:
bool IsEmailConfirmed
,默认为false
。[HttpGet, AllowAnonymous] ConfirmEmail(string email, string token)
,它将验证该令牌是否已保存在数据库中并相应地更新IsEmailConfirmed
。http://YOUR.SERVER/YourController/ConfirmEmail?email={0}&token={1}
,其中{0}
是用户电子邮件,{1}
是您的用户电子邮件确认令牌。它应返回一个视图,告诉确认是否成功。但是,我建议不要重新发明轮子,只需使用Asp.Net Identity 2.0 framework,这将完成所有那些authn& authz的东西给你。
答案 1 :(得分:1)
请在ASP.Net网站上按照以下示例,其中详细说明了如何在注册工作期间发送电子邮件。
此外,我不推荐MD5密码hashinh,因为它很旧,尝试使用SHA 256哈希密码加密。 http://forums.asp.net/t/1211478.aspx?How+do+I+use+Sha256+to+Encrypt+a+String+