public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            Shop_sys_user user = new Shop_sys_user();

            //获取用户
            try
            {
                if (_redisCacheManager.Get <object>("Redis.sysuser") != null)
                {
                    user = _redisCacheManager.Get <Shop_sys_user>("Redis.sysuser");
                }
                else
                {
                    user = await _userServices.GetSysUserByLoginNameAsync(context.UserName);

                    _redisCacheManager.Set("Redis.sysuser", user, TimeSpan.FromHours(10));
                }
            }
            catch (Exception e)
            {
                _redisCacheManager.Set("Redis.sysuser", user, TimeSpan.FromHours(10));
            }
            if (user != null)
            {
                if (user.uStatus == 0)
                {
                    context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "您已被禁止登陆,请与工作人员联系!");
                    return;
                }
                var _pass       = $"{context.Password}{user.passkey}";
                var _md5passkey = MD5Helper.MD5Encrypt32(_pass);
                if (_md5passkey.Equals(user.passValue))
                {
                    context.Result = new GrantValidationResult(
                        subject: context.UserName,
                        authenticationMethod: "custom",
                        claims: new Claim[]
                    {
                        new Claim("uid", user.uid + ""),
                        new Claim(JwtClaimTypes.Name, user.loginName),
                        new Claim(JwtClaimTypes.GivenName, user.realName),
                        new Claim(JwtClaimTypes.FamilyName, user.nickName),
                        new Claim(JwtClaimTypes.Email, string.IsNullOrWhiteSpace(user.Email)?"":user.Email),
                        new Claim(JwtClaimTypes.Role, "user")
                    });
                    return;
                }
                else
                {
                    context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "密码不正确!");
                    return;
                }
            }
            else
            {
                //验证失败
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "不存在该用户名!");
                return;
            }
        }
Пример #2
0
        public async Task <JsonResult <LoginUserDTO> > ShopMallLogin([FromBody] LoginParameterModel obj)
        {
            var model = new JsonResult <LoginUserDTO>
            {
                ret     = 0,
                Success = true
            };

            #region 参数检验
            if (string.IsNullOrWhiteSpace(obj.LoginName) || string.IsNullOrWhiteSpace(obj.Password))
            {
                model.ret    = 1;
                model.Result = "参数有误!";
                return(model);
            }
            #endregion
            var user = await _userServices.GetSysUserByLoginNameAsync(obj.LoginName.Trim());

            if (user == null)
            {
                model.ret    = 1;
                model.Result = "不存在该用户!";
                return(model);
            }

            try
            {
                #region 请求IdentityServer4,校验用户名密码
                IDictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("username", obj.LoginName.Trim());
                parameters.Add("password", obj.Password.Trim());
                parameters.Add("grant_type", "password");
                parameters.Add("client_id", "client2");
                parameters.Add("client_secret", "secret");
                var loginMsg = GetNetData.DoPost("http://shopmall.identityserver.com/connect/token", parameters, "UTF-8");
                #endregion
                if (loginMsg.IndexOf("error_description") > -1)
                {
                    var error = JsonHelper.ParseFormByJson <ErrorViewModel>(loginMsg);
                    model.ret    = 1;
                    model.Result = error.error_description;
                }
                else
                {
                    var          result       = JsonHelper.ParseFormByJson <ResultViewModel>(loginMsg);
                    LoginUserDTO loginUserDTO = new LoginUserDTO()
                    {
                        headPortrait = string.IsNullOrEmpty(user.headPortrait) ? "默认头像" : user.headPortrait,
                        mobile       = user.Mobile,
                        nickname     = user.nickName,
                        token        = $"{result.TokenType} {result.Token}",
                        uid          = user.uid
                    };
                    await _User_LogServices.LoginSuccessServiceAsync(user.uid, 1, $"{user.loginName}登录系统", obj.Source);

                    model.Result  = "登录成功!";
                    model.Content = loginUserDTO;
                }
            }
            catch (Exception e)
            {
                model.ret    = 3;
                model.Result = "e:" + e;
            }
            return(model);
        }