public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone    = context.Request.Raw["phone"];
            var authCode = context.Request.Raw["authCode"];
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            //检测是否为空
            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(authCode))
            {
                context.Result = errorValidationResult;
                return;
            }

            //验证码
            if (!await authCodeService.Validate(phone, authCode))
            {
                context.Result = errorValidationResult;
                return;
            }

            //完成用户注册
            int userId = await userService.CheckOrCreate(phone);

            if (userId <= 0)
            {
                context.Result = errorValidationResult;
                return;
            }

            context.Result = new GrantValidationResult(userId.ToString(), GrantType);
        }
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code))
            {
                context.Result = errorValidationResult;
                return;
            }
            //检查验证码
            if (!_authCodeService.Validate(phone, code))
            {
                context.Result = errorValidationResult;
                return;
            }
            //完成用户注册
            var userInfo = await _userService.CheckOrCreateAsync(phone);

            if (userInfo == null)
            {
                context.Result = errorValidationResult;
                return;
            }

            var claims = new Claim[] {
                new Claim("name", userInfo.Name ?? string.Empty),
                new Claim("company", userInfo.Company ?? string.Empty),
                new Claim("title", userInfo.Title ?? string.Empty),
                new Claim("avatar", userInfo.Avatar ?? string.Empty)
            };

            context.Result = new GrantValidationResult(userInfo.userID.ToString(), GrantType, claims);
        }
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code))
            {
                errorValidationResult.ErrorDescription = "手机或者验证码为空错误";
                context.Result = errorValidationResult;
                return;
            }
            //检查验证码
            if (!_authCodeService.Validate(phone, code))
            {
                errorValidationResult.ErrorDescription = "手机或者验证码错误";
                context.Result = errorValidationResult;
                return;
            }
            //完成用户注册
            var userId = await _userService.CheckOrCreateAsync(phone);

            if (userId <= 0)
            {
                errorValidationResult.ErrorDescription = "检查或创建用户错误";
                context.Result = errorValidationResult;
                return;
            }
            context.Result = new GrantValidationResult(userId.ToString(), GrantType);
        }
Beispiel #4
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone     = context.Request.Raw["phone"];
            var auth_code = context.Request.Raw["auth_code"];
            var err       = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(auth_code))
            {
                context.Result = err;
                return;
            }

            if (!_authCodeService.Validate(phone, auth_code))
            {
                context.Result = err;
                return;
            }
            var userid = await _userService.CheckOrCreate(phone);

            if (userid <= 0)
            {
                context.Result = err;
                return;
            }

            context.Result = new GrantValidationResult(userid.ToString(), GrantType);
        }
Beispiel #5
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone    = context.Request.Raw["phone"];
            var authcode = context.Request.Raw["authcode"];

            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(authcode))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant);
                return;
            }
            if (!_authCodeService.Validate(phone, authcode))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant);
                return;
            }
            var user = await _userService.CheckOrCreate(phone);

            if (user != null)
            {
                Claim[] claims = new Claim[] {
                    new Claim("name", user.Name ?? string.Empty),
                    new Claim("company", user.Company ?? string.Empty),
                    new Claim("title", user.Title ?? string.Empty),
                    new Claim("avatar", user.Avatar ?? string.Empty)
                };
                //获取用户信息 放入 Claim[], 也可以 在 ProfileService中的验证方法中 获取用户信息 放入 Claim[]
                //这里 已经获取了用户信息,所有直接就完成上述操作
                context.Result = new GrantValidationResult(user.UserId.ToString(), GrantType, claims);
                return;
            }
            context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant);
        }
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var tel  = context.Request.Raw["tel"];
            var code = context.Request.Raw["auth_code"];
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrEmpty(tel) || string.IsNullOrEmpty(code))
            {
                context.Result = errorValidationResult;
            }
            if (!_authCodeService.Validate(tel, code))
            {
                context.Result = errorValidationResult;
            }
            var userInfo = await _userService.CheckOrCreate(tel);

            if (userInfo == null)
            {
                context.Result = errorValidationResult;
            }
            var claims = new Claim[] {
                new Claim("name", userInfo.Name ?? string.Empty),
                new Claim("company", userInfo.Company ?? string.Empty),
                new Claim("title", userInfo.Title ?? string.Empty),
                new Claim("avatar", userInfo.Avatar ?? string.Empty),
            };
            var grantResult = new GrantValidationResult(userInfo.Id.ToString(), GrantType, claims);

            context.Result = grantResult;
        }
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone            = context.Request.Raw["phone"];
            var sms_code         = context.Request.Raw["auth_code"];
            var validationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(sms_code))
            {
                context.Result = validationResult;
                return;
            }
            if (!authCodeService.Validate(phone, sms_code))
            {
                context.Result = validationResult;
                return;
            }
            int userId = await userService.CheckOrCreate(phone);

            if (userId <= 0)
            {
                context.Result = validationResult;
                return;
            }

            context.Result = new GrantValidationResult(userId.ToString(), GrantType);
        }
Beispiel #8
0
        //connect/token 访问进来
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];

            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code))
            {
                context.Result = errorValidationResult;
                return;
            }
            //检查手机号和验证码是否匹配
            if (!await _authCodeService.Validate(phone, code))
            {
                context.Result = errorValidationResult;
                return;
            }
            //var userInfo = await _userService.GetOrCreateAsync(phone);
            //if (userInfo == null)
            //{
            //    //如果用户ID小于等于0 ,验证失败
            //    context.Result = errorValidationResult;
            //    return;
            //}

            #region 测试代码
            var userInfo = new BaseUserInfo();
            userInfo.Name    = "luoyi";
            userInfo.Title   = "denglu";
            userInfo.Company = "hw";
            userInfo.Avatar  = "2222";
            #endregion

            //构建UserClaims
            var claims = new Claim[]
            {
                new Claim("name", userInfo.Name ?? string.Empty),
                new Claim("title", userInfo.Title ?? string.Empty),
                new Claim("company", userInfo.Company ?? string.Empty),
                new Claim("avatar", userInfo.Avatar ?? string.Empty)
            };
            context.Result = new GrantValidationResult(userInfo.UserId.ToString(), GrantType, claims);
        }
Beispiel #9
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            //从请求中获得 手机号和验证码
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];

            //授权失败
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            //检查手机号和验证码参数是否符合预期
            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code))
            {
                context.Result = errorValidationResult;
                return;
            }

            //检测验证码
            if (!await _authCodeService.Validate(phone, code))
            {
                context.Result = errorValidationResult;
                return;
            }

//            //构建UserClaims
//            var claims = new Claim[]
//            {
//                new Claim("name","gzz"),
//                new Claim(phone,code),
//            };
//            context.Result = new GrantValidationResult("gzz_id", GrantType, claims);

            //完成用户注册
            var userId = await _userService.CheckOrCreate(phone);

            if (userId <= 0)
            {
                context.Result = errorValidationResult;
                return;
            }

            context.Result = new GrantValidationResult(userId.ToString(), GrantType);
        }
Beispiel #10
0
        /// <summary>
        /// 验证用户获取tokcer
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];

            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            //判断手机号和验证码
            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code))
            {
                //返回错误的状态
                context.Result = errorValidationResult;
                return;
            }
            //验证验证码
            if (!_authCodeService.Validate(phone, authCode: code))
            {
                context.Result = errorValidationResult;
                return;
            }

            var userInfo = await _userService.CheckOrCreateAsync(phone);

            if (userInfo == null)
            {
                context.Result = errorValidationResult;
                return;
            }
            //赋值
            List <Claim> list = new List <Claim>()
            {
                new Claim("name", userInfo.Name ?? string.Empty),
                new Claim("company", userInfo.Company ?? string.Empty),
                new Claim("phone", userInfo.Phone ?? string.Empty),
                new Claim("title", userInfo.Title ?? string.Empty),
                new Claim("avatar", userInfo.Avatar ?? string.Empty),
            };

            context.Result = new GrantValidationResult(userInfo.Id.ToString(), GrantType, list);
        }
Beispiel #11
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];

            //由于这里定义的是InvalidGrant,所以再请求时,无论时什么错误都会抛出InvalidGrant错误
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);


            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code))
            {
                context.Result = errorValidationResult;
            }
            //检查验证码
            if (!_authCodeService.Validate(phone, code))
            {
                context.Result = errorValidationResult;
                return;
            }

            //完成用户注册
            var userInfo = await _userService.CheckOrCreate(phone);//如果请求路径不对,例如在UserService的_userServiceUrl的变量中忘记添加http://执行到这里就不会继续进行了,并在postman中提示 "error": "invalid_grant"

            if (userInfo == null)
            {
                context.Result = errorValidationResult;
                return;
            }

            var claims = new Claim[] {
                new Claim("name", userInfo.Name ?? string.Empty),
                new Claim("company", userInfo.Company ?? string.Empty),
                new Claim("title", userInfo.Title ?? string.Empty),
                new Claim("avatar", userInfo.Avatar ?? string.Empty),
            };

            //context.Result = new GrantValidationResult(userId.ToString(), GrantType);

            context.Result = new GrantValidationResult(userInfo.UserId.ToString(), GrantType, claims);
        }
Beispiel #12
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            //从请求中获得 手机号和验证码
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];

            //授权失败
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            //检查手机号和验证码参数是否符合预期
            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code))
            {
                context.Result = errorValidationResult;
                return;
            }
            //检查手机号和验证码是否匹配
            if (!await _authCodeService.Validate(phone, code))
            {
                context.Result = errorValidationResult;
                return;
            }
            var userinfo = await _userService.GetOrCreateAsync(phone);

            if (userinfo == null)
            {
                //如果用户ID小于等于0 ,验证失败
                context.Result = errorValidationResult;
                return;
            }
            //构建UserClaims
            var claims = new Claim[]
            {
                new Claim("name", userinfo.Name ?? string.Empty),
                new Claim("title", userinfo.Title ?? string.Empty),
                new Claim("company", userinfo.Company ?? string.Empty),
                new Claim("avatar", userinfo.Avatar ?? string.Empty)
            };

            context.Result = new GrantValidationResult(userinfo.UserId.ToString(), GrantType, claims);
        }
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone               = context.Request.Raw["phone"];
            var authcode            = context.Request.Raw["auth_code"];
            var errorValidateResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(authcode))
            {
                context.Result = errorValidateResult;
                return;
            }

            //校验验证码
            if (!_authCodeService.Validate(phone, authcode))
            {
                context.Result = errorValidateResult;
                return;
            }

            //校验手机号是否存在,
            var userInfoDto = await _userService.CheckOrCreateAsync(phone);

            if (userInfoDto == null)
            {
                context.Result = errorValidateResult;
                return;
            }

            var cliams = new List <Claim> {
                new Claim("UserId", $"{userInfoDto?.Id}"),
                new Claim(nameof(userInfoDto.Name), $"{userInfoDto?.Name}"),
                new Claim(nameof(userInfoDto.Company), $"{userInfoDto?.Company}"),
                new Claim(nameof(userInfoDto.Title), $"{userInfoDto?.Title}"),
                new Claim(nameof(userInfoDto.Phone), $"{userInfoDto?.Phone}"),
                new Claim(nameof(userInfoDto.Avatar), $"{userInfoDto?.Avatar}"),
            };

            context.Result = new GrantValidationResult(userInfoDto.Id.ToString(), GrantType, cliams);
        }
        /// <summary>
        /// 进行验证的主要方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];
            //设一个错误结果,当认证失败时,返回该结果
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code))
            {
                context.Result = errorValidationResult;
                return;
            }

            //检查验证码是否正确
            if (!_authCodeService.Validate(phone, code))
            {
                context.Result = errorValidationResult;
                return;
            }

            //检查用户是否存在,不存在创建用户
            UserInfo userInfo = await _userService.CheckOrCreate(phone);

            if (userInfo == null)
            {
                context.Result = errorValidationResult;
                return;
            }

            //添加claim,这些值会放在返回的jwt里
            Claim[] claims = new Claim[] {
                new Claim("name", userInfo.Name == null?string.Empty:userInfo.Name),
                new Claim("company", userInfo.Company == null?string.Empty:userInfo.Company),
                new Claim("avatar", userInfo.Avatar == null?string.Empty:userInfo.Avatar),
                new Claim("title", userInfo.Title == null?string.Empty:userInfo.Title),
            };
            //返回认证成功,用户ID(subject)和认证类型
            context.Result = new GrantValidationResult(userInfo.Id.ToString(), GrantType, claims);
        }
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];
            var error = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            //验证验证码
            if (!_authCodeService.Validate(phone, code))
            {
                context.Result = error;
                return;
            }

            //用户注册
            var userId = await _userService.CheckOrCreate(phone);

            if (userId <= 0)
            {
                context.Result = error;
                return;
            }

            context.Result = new GrantValidationResult(userId.ToString(), GrantType);
        }
Beispiel #16
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone = context.Request.Raw["phone"];
            var code  = context.Request.Raw["auth_code"];

            if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "手机号或验证码不能为空");
                return;
            }
            if (!_authCodeService.Validate(phone, code))
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "无效的验证码");
                return;
            }
            int id = await _userService.CreateOrCheckAsync(phone);

            if (id <= 0)
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "获取userid失败");
                return;
            }
            context.Result = new GrantValidationResult(id.ToString(), GrantType);
        }
Beispiel #17
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            var phone                 = context.Request.Raw["phone"];
            var code                  = context.Request.AuthorizationCode;
            var auth_code             = context.Request.Raw["auth_code"];
            var errorValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant);

            if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(auth_code))
            {
                context.Result = errorValidationResult;
                return;
            }
            //验证手机号码
            if (!authCodeServce.Validate(phone, auth_code))
            {
                context.Result = errorValidationResult;
                return;
            }
            //验证用户
            var userInfo = await userServices.CheckOrCreate(phone);

            if (userInfo == null)
            {
                context.Result = errorValidationResult;
                return;
            }
            //返回正确的结果
            var claims = new Claim[] {
                new Claim("Name", userInfo.Name ?? string.Empty),
                new Claim("Company", userInfo.Company),
                new Claim("Title", userInfo.Title),
                new Claim("Avatar", userInfo.Avatar)
            };

            context.Result = new GrantValidationResult(userInfo.Id.ToString(), GrantType, claims);
        }