Esempio n. 1
0
        /// <summary>
        /// 发送手机验证码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="mobile">手机号码</param>
        /// <param name="system">系统</param>
        /// <returns>验证码</returns>
        /// <returns>发送是否正常</returns>
        public bool GetVerificationCode(BaseUserInfo userInfo, string mobile, string system = "中通中天核心系统", string channel = "3")
        {
            // 应用编号
            ApplicationCode = "ZhongTian";
            // 短信发送账户编号
            AccountCode = "ZhongTian";
            // 应用密码
            Password = "******";

            bool result = false;

            if (string.IsNullOrEmpty(system))
            {
                system = "中通中天核心系统";
            }
            // todo 需要增加一天只能收取几次验证码的限制,8个小时内最多只能发送3次验证码
            int sendVerificationCodeCount = GetSendVerificationCodeCount(mobile);

            if (sendVerificationCodeCount < 6)
            {
                // 产生随机验证码、数字的、六位长度
                int code = new Random().Next(100000, 999999);
                // 发送的手机短信
                string message   = "您在" + system + "手机验证码为:" + code.ToString();
                string returnMsg = string.Empty;
                result = SendMobile(userInfo, "Base", "VerificationCode", mobile, message, code.ToString(), true, false, channel, out returnMsg) > 0;
                if (result && userInfo != null)
                {
                    var userLogOnManager = new BaseUserLogOnManager(userInfo);
                    userLogOnManager.SetProperty(userInfo.Id, new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldVerificationCode, code));
                }
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 设置用户主管的审核状态
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <param name="auditStates">审核状态</param>
        /// <returns>影响行数</returns>
        public int SetUserManagerAuditStates(BaseUserInfo userInfo, string[] ids, AuditStatus auditStates)
        {
            int result = 0;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();
                var userManager = new BaseUserManager(dbHelper, userInfo);
                // 被审核通过
                if (auditStates == AuditStatus.AuditPass)
                {
                    parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldManagerAuditStatus, auditStates.ToString()));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldManagerAuditDate, DateTime.Now));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldEnabled, 1));
                    result = userManager.SetProperty(ids, parameters);

                    // 锁定时间需要去掉
                    // 密码错误次数需要修改掉
                    var userLogOnManager = new BaseUserLogOnManager(dbHelper, userInfo);
                    parameters           = new List <KeyValuePair <string, object> >();
                    parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldLockStartDate, null));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldLockEndDate, null));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldUserOnLine, 0));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldPasswordErrorCount, 0));
                    result = userLogOnManager.SetProperty(ids, parameters);

                    // var staffManager = new BaseStaffManager(dbHelper, result);
                    // string[] staffIds = staffManager.GetIds(BaseStaffEntity.FieldUserId, ids);
                    // staffManager.SetProperty(staffIds, new KeyValuePair<string, object>(BaseStaffEntity.FieldEnabled, 1));
                }
                // 被退回
                if (auditStates == AuditStatus.AuditReject)
                {
                    parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldManagerAuditStatus, auditStates.ToString()));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldManagerAuditDate, DateTime.Now));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldEnabled, 0));
                    parameters.Add(new KeyValuePair <string, object>(BaseUserEntity.FieldAuditStatus, auditStates.ToString()));
                    result = userManager.SetProperty(ids, parameters);
                }
            });

            return(result);
        }
        /// <summary>
        /// 检查用户的 macAddress 绑定是否正常
        ///
        /// 防止重复多读数据?
        /// 是否判断正确?
        /// 可以按每个用户缓存?
        /// 若没有就自动化增加?
        /// mac 限制完善?
        /// mac 限制缓存预热?
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="macAddress">硬件地址</param>
        /// <returns>正确</returns>
        public static bool CheckMACAddressByCache(string userId, string macAddress)
        {
            // 默认是不成功的,防止出错误
            bool result = false;

            // 检查参数的有效性
            if (string.IsNullOrEmpty(userId))
            {
                return(result);
            }
            if (string.IsNullOrEmpty(macAddress))
            {
                return(result);
            }

            // 提高效率,全小写转换
            macAddress = macAddress.ToLower();

            // 这里是处理,多个mac的问题
            string[] mac = macAddress.Split(';');

            using (var redisClient = PooledRedisHelper.GetClient())
            {
                string key = "MAC:" + userId;

                // 若是缓存里过期了?
                if (!redisClient.ContainsKey(key))
                {
                    // 重新缓存用户的限制数据
                    if (CachePreheatingMACAddressByUser(redisClient, userId) == 0)
                    {
                        // 若没有设置mac限制,需要把限制都自动加上来。
                        // 没有加到数据的,就是表明是新增加的用户、第一次登录的用户
                        BaseParameterManager parameterManager = new BaseParameterManager();
                        for (int i = 0; i < mac.Length; i++)
                        {
                            if (!string.IsNullOrEmpty(mac[i]))
                            {
                                // 把收集过来的mac地址需要保存起来
                                BaseParameterEntity parameterEntity = new BaseParameterEntity();
                                parameterEntity.Id            = Guid.NewGuid().ToString("N");
                                parameterEntity.CategoryCode  = "MacAddress";
                                parameterEntity.ParameterCode = "Single";
                                parameterEntity.ParameterId   = userId;
                                // 这里之际保存小写、就效率也高,省事了
                                parameterEntity.ParameterContent = mac[i].Trim();
                                parameterManager.Add(parameterEntity);
                            }
                        }
                        result = true;
                    }
                }

                // 若还是没有?表示是新增的
                if (redisClient.ContainsKey(key))
                {
                    // 若已经存在,就需要进行缓存里的判断?
                    // 这里要提高效率,不能反复打开缓存
                    for (int i = 0; i < mac.Length; i++)
                    {
                        // 这里对数据还不放心,进行优化处理
                        if (!string.IsNullOrEmpty(mac[i]))
                        {
                            mac[i] = mac[i].Trim();
                            result = redisClient.SetContainsItem(key, mac[i]);
                            if (result)
                            {
                                // 这里要提高判断的效率
                                break;
                            }
                        }
                    }
                    // 若没有验证成功、把当前的 macAddress 保存起来, 方便后台管理的人加上去。
                    if (!result)
                    {
                        List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();
                        parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldMACAddress, macAddress));
                        BaseUserLogOnManager userLogOnManager = new BaseUserLogOnManager();
                        userLogOnManager.SetProperty(userId, parameters);
                    }
                }
            }

            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="userId">被设置的用户主键</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="unlock">解除锁定</param>
        /// <param name="autoAdd">数据缺少自动补充登录信息</param>
        /// <returns>影响行数</returns>
        public virtual int SetPassword(string userId, string newPassword, bool?unlock = null, bool?autoAdd = null, bool modifyRecord = true)
        {
            int result = 0;

            // 密码强度检查

            /*
             * if (BaseSystemInfo.CheckPasswordStrength)
             * {
             *  if (password.Length == 0)
             *  {
             *      this.StatusCode = StatusCode.PasswordCanNotBeNull.ToString();
             *      return result;
             *  }
             * }
             */
            string encryptPassword = newPassword;
            string salt            = string.Empty;

            // 加密密码
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                salt            = BaseRandom.GetRandomString(20);
                encryptPassword = this.EncryptUserPassword(newPassword, salt);
            }
            // 设置密码字段
            List <KeyValuePair <string, object> > parameters = new List <KeyValuePair <string, object> >();

            parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldUserPassword, encryptPassword));
            // 需要重新登录才可以,防止正在被人黑中,阻止已经在线上的人
            parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldOpenId, Guid.NewGuid().ToString("N")));
            if (BaseSystemInfo.ServerEncryptPassword)
            {
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldSalt, salt));
            }
            parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldChangePasswordDate, DateTime.Now));
            if (unlock.HasValue && unlock.Value == true)
            {
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldEnabled, 1));
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldLockStartDate, null));
                parameters.Add(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldLockEndDate, null));
            }
            BaseUserLogOnManager userLogOnManager = new BaseUserLogOnManager(this.DbHelper, this.UserInfo);

            result = userLogOnManager.SetProperty(new KeyValuePair <string, object>(BaseUserLogOnEntity.FieldId, userId), parameters);
            if (result == 0 && autoAdd.HasValue && autoAdd.Value == true)
            {
                BaseUserLogOnEntity userLogOnEntity = new BaseUserLogOnEntity();
                userLogOnEntity.Id = userId;
                userLogOnEntity.ChangePasswordDate = DateTime.Now;
                userLogOnEntity.UserPassword       = encryptPassword;
                userLogOnEntity.Salt    = salt;
                userLogOnEntity.Enabled = 1;
                userLogOnManager.AddObject(userLogOnEntity);
                result = 1;
            }

            // 2015-12-09 吉日嘎拉 增加日志功能、谁什么时候设置了谁的密码?
            if (modifyRecord)
            {
                var record = new BaseModifyRecordEntity();
                record.TableCode         = BaseUserLogOnEntity.TableName.ToUpper();
                record.TableDescription  = "用户登录信息表";
                record.ColumnCode        = BaseUserLogOnEntity.FieldUserPassword;
                record.ColumnDescription = "用户密码";
                record.RecordKey         = userId;
                record.NewValue          = "设置密码";
                // record.OldValue = "";
                if (this.UserInfo != null)
                {
                    record.IPAddress    = this.UserInfo.IPAddress;
                    record.CreateUserId = this.UserInfo.Id;
                    record.CreateOn     = DateTime.Now;
                }
                BaseModifyRecordManager modifyRecordManager = new Business.BaseModifyRecordManager(this.UserInfo, BaseUserEntity.TableName + "_Log");
                modifyRecordManager.Add(record, true, false);
            }

            if (result == 1)
            {
                this.StatusCode = Status.SetPasswordOK.ToString();
                // 调用扩展
                if (BaseSystemInfo.OnInternet && BaseSystemInfo.ServerEncryptPassword)
                {
                    // AfterSetPassword(userId, salt, password);
                }
            }
            else
            {
                // 数据可能被删除
                this.StatusCode = Status.ErrorDeleted.ToString();
            }

            return(result);
        }