Ejemplo n.º 1
0
        public async Task <TokenValidationResult> ValidateAccessTokenAsync(string token, string expectedScope = null)
        {
            var tokenValues = OAuth1Helper.GetResponseValues(token);

            tokenValues.TryGetValue("access_token", out string accessToken);
            token = accessToken ?? token;

            var endpoint = userInfoEndpoint.Replace(tokenReplacement, token);

            var response = await _client.GetAsync(endpoint);

            if (response.IsSuccessStatusCode)
            {
                var userInfo = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

                var claims = new List <Claim>();
                claims.AddRange(userInfo.RootElement.EnumerateObject().Select(item => new Claim(item.Name, item.Value.ToString())));

                return(new TokenValidationResult
                {
                    IsError = false,
                    Claims = claims
                });
            }

            return(new TokenValidationResult
            {
                IsError = true,
                ErrorDescription = response.ReasonPhrase
            });
        }
        public async Task <TokenValidationResult> ValidateAccessTokenAsync(string token, string expectedScope = null)
        {
            Uri endpoint    = new Uri(userInfoEndpoint);
            var tokenValues = OAuth1Helper.GetResponseValues(token);

            tokenValues.TryGetValue("oauth_token", out string oauthToken);
            tokenValues.TryGetValue("oauth_token_secret", out string oauthTokenSecret);

            string authorizationHeader = _oauthHelper.GetAuthorizationHeader(
                endpoint, "GET", _options.TwitterConsumerAPIKey, _options.TwitterConsumerSecret, oauthToken, oauthTokenSecret, null);

            _client.DefaultRequestHeaders.Clear();
            _client.DefaultRequestHeaders.Accept.Add(OAuth1Helper.GetMediaTypeHeader());
            _client.DefaultRequestHeaders.Add("Authorization", authorizationHeader);

            var response = await _client.GetAsync(endpoint);

            if (response.IsSuccessStatusCode)
            {
                var userInfo = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

                var claims = new List <Claim>();
                claims.AddRange(userInfo.RootElement.EnumerateObject().Select(item => new Claim(item.Name, item.Value.ToString())));

                return(new TokenValidationResult
                {
                    IsError = false,
                    Claims = claims
                });
            }

            return(new TokenValidationResult
            {
                IsError = true,
                ErrorDescription = response.ReasonPhrase
            });
        }