Ejemplo n.º 1
0
        private static async Task <OAuth20AuthResponse> AuthenticateUsingOAuth20Async(HttpRequestMessage httpRequest, OAuth20AuthenticationConfig authConfig)
        {
            var authRequestContent = HttpOAuth20ContentFactory.Create(authConfig);

            HttpRequestMessage authRequest = new HttpRequestMessage
            {
                Content    = authRequestContent,
                Method     = HttpMethod.Post,
                RequestUri = new Uri(authConfig.HttpRequestConfig.AuthUrl),
            };

            authRequest.Headers.UserAgent.TryParseAdd("PostmanRuntime/7.26.8");

            AddDefaultHeaders(authRequest);

            var httpClient   = GetHttpClient();
            var authResponse = await httpClient.SendAsync(authRequest);

            var authResponseContent = await authResponse.Content.ReadAsStringAsync();

            if (authResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Reason: {authResponseContent}, Trying to access resource: {httpRequest.RequestUri}, HttpStatusCode: {authResponse.StatusCode}, Configuration: {JsonConvert.SerializeObject(authConfig)}");
            }

            OAuth20AuthResponse oAuthTokens = JsonConvert.DeserializeObject <OAuth20AuthResponse>(authResponseContent);

            return(oAuthTokens);
        }
Ejemplo n.º 2
0
        private static async Task <OAuth20AuthResponse> AuthenticateUsingOAuth20Async(HttpRequestMessage httpRequest, OAuth20AuthenticationConfig authConfig)
        {
            var authRequestContent = HttpOAuth20ContentFactory.Create(authConfig);

            HttpRequestMessage authRequest = new HttpRequestMessage
            {
                Content    = authRequestContent,
                Method     = HttpMethod.Post,
                RequestUri = new Uri(authConfig.HttpRequestConfig.AuthUrl),
            };

            authRequest.Headers.UserAgent.TryParseAdd("PostmanRuntime/7.26.8");

            authRequest.Headers.TryAddWithoutValidation("Accept", "*/*");
            authRequest.Headers.TryAddWithoutValidation("Cache-Control", "no-cache");
            authRequest.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br");
            authRequest.Headers.TryAddWithoutValidation("Connection", "keep-alive");

            var handler = new HttpClientHandler();

            handler.AllowAutoRedirect = true;
            handler.UseProxy          = false;
            handler.CheckCertificateRevocationList = false;

            var httpClient   = new HttpClient(handler);
            var authResponse = await httpClient.SendAsync(authRequest);

            var authResponseContent = await authResponse.Content.ReadAsStringAsync();

            if (authResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Trying to access resource: {httpRequest.RequestUri}, HttpStatusCode: {authResponse.StatusCode}, Reason: {authResponseContent}, Configuration: {JsonConvert.SerializeObject(authConfig)}");
            }

            OAuth20AuthResponse oAuthTokens = JsonConvert.DeserializeObject <OAuth20AuthResponse>(authResponseContent);

            return(oAuthTokens);
        }