/// <summary>
        /// 用户忘记密码,发送密码
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <param name="status">状态</param>
        /// <param name="statusMessage">状态信息</param>
        /// <param name="newPassword">新密码</param>
        /// <returns>成功发送密码</returns>
        public static bool ResetPassword(string email, out Status status, out string statusMessage, out string newPassword)
        {
            var result = false;

            // 1.用户是否找到?默认是未找到用户状态
            status        = Status.UserNotFound;
            statusMessage = "未找到对应的用户";
            newPassword   = RandomUtil.GetRandom(100000, 999999).ToString();

            var userContactManager = new BaseUserContactManager();
            var parameters         = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>(BaseUserContactEntity.FieldEmail, email)
            };
            var userContactEntity = BaseEntity.Create <BaseUserContactEntity>(userContactManager.GetDataTable(parameters));

            if (userContactEntity != null && userContactEntity.UserId > 0)
            {
                var userManager = new BaseUserManager();
                // 2.用户是否已被删除?
                parameters = new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>(BaseUserEntity.FieldId, userContactEntity.UserId),
                    new KeyValuePair <string, object>(BaseUserEntity.FieldDeleted, 0)
                };
                var userEntity = BaseEntity.Create <BaseUserEntity>(userManager.GetDataTable(parameters));
                // 是否已找到了此用户
                if (userEntity != null && userEntity.Id > 0)
                {
                    // 3.用户是否有效的?
                    if (userEntity.Enabled == 1)
                    {
                        if (userManager.SetPassword(userEntity.Id, newPassword) > 0)
                        {
                            result        = true;
                            status        = Status.Ok;
                            statusMessage = "新密码已发送到您的注册邮箱" + email + ",请注意查收。";
                        }
                        else
                        {
                            status        = Status.ErrorUpdate;
                            statusMessage = "更新数据库失败,请重试!";
                        }
                    }
                    else
                    {
                        if (userEntity.Enabled == 0)
                        {
                            status        = Status.UserLocked;
                            statusMessage = "用户被锁定,不允许重置密码。";
                        }
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// 忘记密码按手机号码获取
        /// </summary>
        /// <param name="applicationCode">应用编号</param>
        /// <param name="accountCode">账户</param>
        /// <param name="password">密码</param>
        /// <param name="userName">用户名</param>
        /// <param name="mobile">手机号码</param>
        /// <returns>成功</returns>
        public bool GetPasswordByMobile(BaseUserInfo userInfo, string userName, string mobile)
        {
            bool result = false;

            if (!string.IsNullOrEmpty(mobile))
            {
                BaseUserContactManager manager = new BaseUserContactManager();
                List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();
                if (!string.IsNullOrEmpty(mobile))
                {
                    parameters.Add(new KeyValuePair <string, object>(BaseUserContactEntity.FieldMobile, mobile));
                }
                // 手机号码重复不发验证码,防止把别人的密码给修改了
                DataTable dt = manager.GetDataTable(parameters);
                string    id = string.Empty;
                if (dt != null && dt.Rows.Count == 1)
                {
                    id = dt.Rows[0][BaseUserContactEntity.FieldId].ToString();
                }
                BaseUserManager userManager = null;
                if (!string.IsNullOrEmpty(id))
                {
                    userManager = new BaseUserManager();
                    bool           userNameOK = true;
                    BaseUserEntity userEntity = userManager.GetObject(id);
                    if (!string.IsNullOrEmpty(userName))
                    {
                        if (!string.IsNullOrEmpty(userEntity.UserName) && !userEntity.UserName.Equals(userName))
                        {
                            userNameOK = false;
                            userInfo   = null;
                        }
                    }
                    // 只有有效的用户,才能获取密码,被删除的,无效的,不可以获取密码
                    if (userEntity.Enabled == 0 || userEntity.DeletionStateCode == 1)
                    {
                        userNameOK = false;
                        userInfo   = null;
                    }
                    if (userNameOK)
                    {
                        userInfo = userManager.ConvertToUserInfo(userEntity);
                    }
                    else
                    {
                        userInfo = null;
                    }
                }
                if (!string.IsNullOrEmpty(id) && userInfo != null)
                {
                    string userPassword = string.Empty;
                    if (BaseSystemInfo.CheckPasswordStrength)
                    {
                        userPassword = BaseRandom.GetRandomString(8).ToLower();
                    }
                    else
                    {
                        userPassword = BaseRandom.GetRandomString(8).ToLower();
                        // Random random = new System.Random();
                        // userPassword = random.Next(100000, 999999).ToString();
                    }
                    // 看是否有合理的请求参数
                    if (!string.IsNullOrEmpty(userPassword))
                    {
                        // 看是否一天超过了3次了
                        int sendUserPasswordCount = this.GetSendUserPasswordCount(mobile);
                        if (sendUserPasswordCount < 4)
                        {
                            // 应用编号
                            if (this.SendUserPassword(userInfo, mobile, userPassword))
                            {
                                userManager = new BaseUserManager(userInfo);
                                // 按手机号码获取的,可以自动解锁,防止密码连续输入错误,然后手机号码获取密码后,是被锁定状态,提高工作效率
                                userManager.SetPassword(userInfo.Id, userPassword, true);
                                userManager.GetStateMessage();
                                if (userManager.StatusCode == Status.SetPasswordOK.ToString())
                                {
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }