Пример #1
0
        public IActionResult Auth([FromBody] UserSignInViewModel userSignInViewModel)
        {
            Models.Response.ResponseModel response = ResponseModelFactory.CreateInstance;
            DncUser user;

            user = _dbContext.DncUser.FirstOrDefault(x => x.LoginName == userSignInViewModel.UserName.Trim());

            string[] passwordSplit = user.Password.Split("$");

            if (user == null)
            {
                response.SetFailed("用户不存在");
                return(Ok(response));
            }

            if (passwordSplit[1] != PasswordCalculator.SaltPassword(passwordSplit[0], userSignInViewModel.PasswordMD5))
            {
                response.SetFailed("密码不正确");
                return(Ok(response));
            }

            try
            {
                response.SetData(TokenBulider(user));
                return(Ok(response));
            }
            catch (InvalidOperationException ex)
            {
                response.SetFailed(ex.Message);
                return(Ok(response));
            }
        }
Пример #2
0
        public IActionResult SignUp([FromBody] UserSignUpViewModel createBody)
        {
            UserCreateViewModel user = new UserCreateViewModel
            {
                LoginName   = createBody.LoginName,
                DisplayName = createBody.DisplayName ?? createBody.LoginName,
                IsLocked    = CommonEnum.IsLocked.UnLocked,
                Status      = UserStatus.Normal,
                UserType    = UserType.GeneralUser,
                IsDeleted   = CommonEnum.IsDeleted.No,
            };
            string passwordMD5 = createBody.PasswordMD5;

            user.Password = $"{user.LoginName}${PasswordCalculator.SaltPassword(user.LoginName, passwordMD5)}";

            return(Create(user));
        }
Пример #3
0
        public IActionResult ExposeChangePassword([FromBody] ChangePasswordViewModel changePassword)
        {
            ResponseModel response = ResponseModelFactory.CreateInstance;
            DncUser       user     = _dbContext.DncUser.First(x => x.Guid == AuthContextService.CurrentUser.Guid);

            string[] passwordSplit = user.Password.Split("$");
            if (passwordSplit[1] != PasswordCalculator.SaltPassword(passwordSplit[0], changePassword.OldPasswordMD5))
            {
                response.SetFailed("旧密码错误");
                return(Ok(response));
            }
            else
            {
                _dbContext.DncUser.First(x => x.Guid == AuthContextService.CurrentUser.Guid).Password = user.Password = $"{user.LoginName}${PasswordCalculator.SaltPassword(user.LoginName, changePassword.NewPasswordMD5)}";
                _dbContext.SaveChanges();
                response.SetSuccess();
                return(Ok(response));
            }
        }
Пример #4
0
        public IActionResult AutoCreate([FromBody] AutoCreateUserViewModel model, bool autoPassword = true, bool returnToken = false)
        {
            ResponseModel response = ResponseModelFactory.CreateInstance;

            //查找用户是否已经存在
            DncUser target = _dbContext.DncUser.FirstOrDefault(x => (!string.IsNullOrWhiteSpace(model.WeChatUnionID) && x.WeChatUnionID == model.WeChatUnionID) || x.PhoneNumber == model.PhoneNumber);

            //不存在时自动创建
            if (target == default)
            {
                UserCreateViewModel user = new UserCreateViewModel
                {
                    LoginName   = model.PhoneNumber ?? model.WeChatUnionID,
                    DisplayName = model.PhoneNumber,
                    PhoneNumber = model.PhoneNumber,
                    IsLocked    = IsLocked.UnLocked,
                    Status      = UserStatus.Normal,
                    UserType    = UserType.GeneralUser,
                    IsDeleted   = IsDeleted.No,
                };
                if (autoPassword)
                {
                    string password = model.PhoneNumber ?? DateTime.Now.ToString();
                    user.Password = model.PhoneNumber != null ? $"{user.LoginName}${PasswordCalculator.SaltPassword(user.LoginName, PasswordCalculator.Md5(password))}" : null;
                }
                Create(user);
                target = _dbContext.DncUser.FirstOrDefault(x => (!string.IsNullOrWhiteSpace(model.WeChatUnionID) && x.WeChatUnionID.Trim() == model.WeChatUnionID) || x.PhoneNumber.Trim() == model.PhoneNumber);
            }

            //依然为default则创建失败
            if (target == default)
            {
                response.SetFailed("用户创建失败");
            }
            else
            {
                //如果Permission不为空,则检查是否具有对应的权限位
                if (!string.IsNullOrWhiteSpace(model.Permission))
                {
                    IQueryable <DncUserRoleMapping> targetPermission = from role in _dbContext.DncUserRoleMapping
                                                                       where role.UserGuid == target.Guid
                                                                       join permission in _dbContext.DncRolePermissionMapping on role.RoleCode equals permission.RoleCode
                                                                       where permission.PermissionCode == model.Permission
                                                                       select role;
                    if (targetPermission.FirstOrDefault() == default)
                    {
                        _dbContext.DncUserRoleMapping.Add(new DncUserRoleMapping
                        {
                            CreatedOn = DateTime.Now,
                            UserGuid  = target.Guid,
                            RoleCode  = model.Permission
                        });
                        _dbContext.SaveChanges();
                    }
                }

                //创建返回体
                dynamic result = new ExpandoObject();
                result.Guid = target.Guid;
                if (returnToken)
                {
                    result.Authorization = TokenBulider(target);
                }
                response.SetData(result);
            }
            return(Ok(response));


            string TokenBulider(DncUser user)
            {
                if (user == null || user.IsDeleted == IsDeleted.Yes)
                {
                    throw new InvalidOperationException("用户不存在");
                }

                if (user.IsLocked == IsLocked.Locked)
                {
                    throw new InvalidOperationException("账号已被锁定");
                }

                if (user.Status == UserStatus.Forbidden)
                {
                    throw new InvalidOperationException("账号已被禁用");
                }

                ClaimsIdentity claimsIdentity = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.LoginName),
                    new Claim("guid", user.Guid.ToString()),
                    new Claim("avatar", ""),
                    new Claim("displayName", user.DisplayName),
                    new Claim("loginName", user.LoginName),
                    new Claim("emailAddress", ""),
                    new Claim("guid", user.Guid.ToString()),
                    new Claim("userType", ((int)user.UserType).ToString()),
                });

                return(JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity));
            }
        }