密码邮箱验证
/// <summary>
/// 重置密码
/// </summary>
/// <param name="username"></param>
/// <param name="email"></param>
/// <returns></returns>
public ResetPasswordStatus ResetPassword(string username, string email)
{
MembershipUserCollection users = Membership.FindUsersByName(username);
if (users.Count <= 0)//用户名未找到
{
//不要在BLL、DAL等非UI层来进行UI层的操作(比如MessageBox、Redirect)
return ResetPasswordStatus.UserNameNotFound;
}
else if (users.Count > 1)
{
throw new Exception("存在多个重名的用户名:"+username);
}
MembershipUser user = users[username];
if (email != user.Email)//Email不匹配
{
return ResetPasswordStatus.EmailInvalid;
}
//重置密码
string newPwd = user.ResetPassword();//重置为新密码
//生成一个简单的随机的新密码
//一种思路是用生成验证码的随机数算法
//或者也可以使用取巧的办法,就是取Guid的后六位
string guid = Guid.NewGuid().ToString();
string newSimplePwd = guid.Substring(guid.Length - 6);
user.ChangePassword(newPwd, newSimplePwd); //把ResetPassword返回的复杂的密码改成新密码
CommonHelper.SendEmail(user.Email,
"您在xx网的新密码", "。。新密码是:" + newSimplePwd);
return ResetPasswordStatus.Success;
}