Beispiel #1
0
        public async Task ValidateAsync(ExtensionGrantValidationContext context)
        {
            //throw new NotImplementedException();
            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.Validatae(phone, code))
            {
                context.Result = errorValidationResult;
                return;
            }

            //完成用户注册
            var userInfo = await _userService.CheckOrCreatAsync(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.Id.ToString(), GrantType, claims);
        }