Example #1
0
        public async Task <AadAuthResult> AuthorizeAsync(string username, string password, IEnumerable <string> scopes)
        {
            // Appending 'offline_access' to get back a refresh token as well.
            var scopeEntries = new HashSet <string>(scopes)
            {
                "offline_access"
            };

            var contentParameters = new Dictionary <string, string>
            {
                { "client_id", _clientId },
                { "grant_type", "password" },
                { "scope", string.Join(" ", scopeEntries) },
                { "username", username },
                { "password", password },
                { "resource", _resource }
            };

            var content = new FormUrlEncodedContent(contentParameters);
            var request = new HttpRequestMessage(HttpMethod.Post, _tokenEndpoint);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = content;

            try
            {
                var response = await _httpClient.SendAsync(request);

                var responseString = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var authTokenResponse = JsonConvert.DeserializeObject <AadAuthTokenResponse>(responseString);
                    return(AadAuthResult.Success(authTokenResponse));
                }
                else
                {
                    var errorResponse = JsonConvert.DeserializeObject <ErrorInfo>(responseString);
                    errorResponse.Status = response.StatusCode;
                    return(AadAuthResult.Failed(errorResponse));
                }
            }
            catch (Exception ex)
            {
                return(AadAuthResult.Failed(new ErrorInfo()
                {
                    Error = ex.Message,
                    Description = ex.ToString()
                }));
            }
        }
Example #2
0
        public async Task <AadAuthResult> AuthorizeViaRefreshTokenAsync(string refreshToken)
        {
            var contentParameters = new Dictionary <string, string>
            {
                { "client_id", _clientId },
                { "grant_type", "refresh_token" },
                { "refresh_token", refreshToken }
            };

            var content = new FormUrlEncodedContent(contentParameters);
            var request = new HttpRequestMessage(HttpMethod.Post, _tokenEndpoint);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = content;

            try
            {
                var response = await _httpClient.SendAsync(request);

                var responseString = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var authTokenResponse = JsonConvert.DeserializeObject <AadAuthTokenResponse>(responseString);
                    return(AadAuthResult.Success(authTokenResponse));
                }
                else
                {
                    var errorResponse = JsonConvert.DeserializeObject <ErrorInfo>(responseString);
                    errorResponse.Status = response.StatusCode;
                    return(AadAuthResult.Failed(errorResponse));
                }
            }
            catch (Exception ex)
            {
                return(AadAuthResult.Failed(new ErrorInfo()
                {
                    Error = ex.Message,
                    Description = ex.ToString()
                }));
            }
        }