/// <summary> /// 用户忘记密码,发送密码 /// </summary> /// <param name="userName">用户名</param> /// <param name="returnStatusCode">状态码</param> /// <param name="returnStatusMessage">状态信息</param> /// <returns>成功发送密码</returns> public static bool SendPassword(string userName, out string returnStatusCode, out string returnStatusMessage) { bool returnValue = false; // 1.用户是否找到?默认是未找到用户状态 returnStatusCode = StatusCode.UserNotFound.ToString(); returnStatusMessage = "用户未找到,请重新输入用户名。"; // 用户数据库进行差找用户操作 IDbProvider dbProvider = new SqlProvider(RDIFrameworkDbConection); dbProvider.Open(); PiUserManager userManager = new PiUserManager(dbProvider); // 2.用户是否已被删除? var parameters = new List <KeyValuePair <string, object> >(); parameters.Add(new KeyValuePair <string, object>(PiUserTable.FieldUserName, userName)); parameters.Add(new KeyValuePair <string, object>(PiUserTable.FieldDeleteMark, 0)); var userEntity = BaseEntity.Create <PiUserEntity>(userManager.GetDT(parameters, 0, string.Empty)); dbProvider.Close(); // 是否已找到了此用户 if (userEntity != null && !string.IsNullOrEmpty(userEntity.Id.ToString())) { // 3.用户是否有效的? if (userEntity.Enabled == 1) { if (!string.IsNullOrEmpty(userEntity.Email)) { // 5.重新产生随机密码? // 6.发送邮件给用户? // 7.重新设置用户密码? returnValue = SendPassword(userEntity); returnStatusCode = StatusCode.OK.ToString(); returnStatusMessage = "新密码已发送到您的注册邮箱" + userEntity.Email + "。"; } else { // 4.用户是否有邮件账户? returnStatusCode = StatusCode.UserNotEmail.ToString(); returnStatusMessage = "用户没有电子邮件地址,无法从新设置密码,请您及时联系系统管理员。"; } } else { if (userEntity.Enabled == 0) { returnStatusCode = StatusCode.UserLocked.ToString(); returnStatusMessage = "用户被锁定,不允许设置密码。"; } else { returnStatusCode = StatusCode.UserNotActive.ToString(); returnStatusMessage = "用户还未被激活,不允许设置密码。"; } } } return(returnValue); }
/// <summary> /// 发送密码给指定的邮箱 /// </summary> /// <param name="userEntity">用户实体</param> /// <returns>成功发送邮件</returns> private static bool SendPassword(PiUserEntity userEntity) { bool returnValue = false; IDbProvider dbProvider = new SqlProvider(RDIFrameworkDbConection); UserInfo userInfo = null; try { string password = RandomHelper.GetRandom(100000, 999999).ToString(); using (var mailMessage = new System.Net.Mail.MailMessage()) { // 接收人邮箱地址 mailMessage.To.Add(new System.Net.Mail.MailAddress(userEntity.Email)); mailMessage.Body = GetSendPasswordBody(userEntity, password); mailMessage.From = new System.Net.Mail.MailAddress("*****@*****.**", ".NET快速开发、整合框架"); mailMessage.BodyEncoding = Encoding.GetEncoding("GB2312"); mailMessage.Subject = ".NET快速开发、整合框架 新密码。"; mailMessage.IsBodyHtml = true; var smtpclient = new System.Net.Mail.SmtpClient("SMTP.126.COM", 25); smtpclient.Credentials = new System.Net.NetworkCredential("*****@*****.**", "abcd"); smtpclient.EnableSsl = false; smtpclient.Send(mailMessage); returnValue = true; // 修改用户的密码 // 用户数据库进行差找用户操作 dbProvider.Open(); PiUserManager userManager = new PiUserManager(dbProvider); userInfo = userManager.ConvertToUserInfo(userEntity); userManager.SetParameter(userInfo); // 密码进行加密,读取网站的密钥 password = userManager.EncryptUserPassword(password); userManager.SetPassword(userEntity.Id.ToString(), password); } } catch (System.Exception exception) { // 若有异常,应该需要保存异常信息 CiExceptionManager.LogException(dbProvider, userInfo, exception); returnValue = false; } finally { dbProvider.Close(); } return(returnValue); }
/// <summary> /// 用户注册之后,给用户发的激活账户的邮件 /// </summary> /// <param name="userEntity">用户实体</param> /// <returns>成功发送邮件</returns> public static bool AfterUserRegister(PiUserEntity userEntity) { bool returnValue = false; IDbProvider dbProvider = new SqlProvider(RDIFrameworkDbConection); UserInfo userInfo = null; try { using (var mailMessage = new System.Net.Mail.MailMessage()) { // 接收人邮箱地址 mailMessage.To.Add(new System.Net.Mail.MailAddress(userEntity.Email)); mailMessage.Body = GetAfterUserRegisterBody(userEntity); mailMessage.From = new System.Net.Mail.MailAddress("*****@*****.**", "新密码"); mailMessage.BodyEncoding = Encoding.GetEncoding("GB2312"); mailMessage.Subject = "新密码。"; mailMessage.IsBodyHtml = true; var smtpclient = new System.Net.Mail.SmtpClient(SystemInfo.ErrorReportMailServer) { Credentials = new System.Net.NetworkCredential(SystemInfo.ErrorReportMailUserName, SystemInfo.ErrorReportMailPassword), EnableSsl = false }; smtpclient.Send(mailMessage); returnValue = true; } } catch (System.Exception exception) { // 若有异常,应该需要保存异常信息 CiExceptionManager.LogException(dbProvider, userInfo, exception); returnValue = false; } finally { dbProvider.Close(); } return(returnValue); }
// // 八 读取用户信息,更新用户信息部分 // #region public static PiUserEntity GetUser(string userId) 获取用户信息 /// <summary> /// 获取用户信息 /// </summary> /// <param name="userId">用户主键</param> /// <returns>用户信息</returns> public static PiUserEntity GetUser(string userId) { PiUserEntity userEntity = null; if (!string.IsNullOrEmpty(userId)) { // 这里需要打开用户中心的数据 var dbHelper = new SqlProvider(RDIFrameworkDbConection); dbHelper.Open(); var parameters = new List <KeyValuePair <string, object> >(); parameters.Add(new KeyValuePair <string, object>(PiUserTable.FieldId, userId)); DataTable dtBaseUser = DbCommonLibary.GetDT(dbHelper, PiUserTable.TableName, parameters, 0, null, null); if (dtBaseUser.Rows.Count > 0) { userEntity = BaseEntity.Create <PiUserEntity>(dtBaseUser); userEntity.WorkCategory = dtBaseUser.Rows[0][PiUserTable.FieldWorkCategory].ToString(); userEntity.QICQ = dtBaseUser.Rows[0][PiUserTable.FieldQICQ].ToString(); userEntity.UserAddress = dtBaseUser.Rows[0][PiUserTable.FieldUserAddress].ToString(); } dbHelper.Close(); } return(userEntity); }