コード例 #1
0
        public async Task <AccountSession> SendTokenRequestAsync(string requestBodyString, IHttpProvider httpProvider)
        {
            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, OAuthConstants.MicrosoftAccountTokenServiceUrl);

            httpRequestMessage.Content = new StringContent(requestBodyString, Encoding.UTF8, "application/x-www-form-urlencoded");

            using (var authResponse = await httpProvider.SendAsync(httpRequestMessage).ConfigureAwait(false))
                using (var responseStream = await authResponse.Content.ReadAsStreamAsync().ConfigureAwait(false))
                {
                    var responseValues =
                        httpProvider.Serializer.DeserializeObject <IDictionary <string, string> >(
                            responseStream);

                    if (responseValues != null)
                    {
                        OAuthErrorHandler.ThrowIfError(responseValues);
                        return(new AccountSession(responseValues));
                    }

                    throw new ServiceException(
                              new Error
                    {
                        Code    = OAuthConstants.ErrorCodes.AuthenticationFailure,
                        Message = "Authentication failed. No response values returned from authentication flow."
                    });
                }
        }
コード例 #2
0
        public async Task <string> GetAuthorizationCodeAsync(
            string clientId,
            string returnUrl,
            string[] scopes,
            IWebAuthenticationUi webAuthenticationUi,
            string userId = null)
        {
            if (webAuthenticationUi != null)
            {
                var requestUri = new Uri(
                    this.GetAuthorizationCodeRequestUrl(
                        clientId,
                        returnUrl,
                        scopes,
                        userId));

                var authenticationResponseValues = await webAuthenticationUi.AuthenticateAsync(
                    requestUri,
                    new Uri(returnUrl)).ConfigureAwait(false);

                OAuthErrorHandler.ThrowIfError(authenticationResponseValues);

                string code;
                if (authenticationResponseValues != null && authenticationResponseValues.TryGetValue("code", out code))
                {
                    return(code);
                }
            }

            return(null);
        }
コード例 #3
0
        private async Task <IAuthenticationResult> PromptUserForAuthenticationWithClientCertificateAsync(
            string serviceResourceId,
            string userId)
        {
            IAuthenticationResult authenticationResult = null;

            var clientAssertionCertificate = new ClientAssertionCertificate(this.clientId, this.clientCertificate);
            var userIdentifier             = this.GetUserIdentifierForAuthentication(userId);
            var redirectUri = new Uri(this.returnUrl);

            var requestUri = new Uri(this.oAuthHelper.GetAuthorizationCodeRequestUrl(
                                         this.clientId,
                                         this.returnUrl,
                                         null,
                                         userId));

            var authenticationResponseValues = await webAuthenticationUi.AuthenticateAsync(
                requestUri,
                redirectUri).ConfigureAwait(false);

            OAuthErrorHandler.ThrowIfError(authenticationResponseValues);

            string code;

            if (authenticationResponseValues != null && authenticationResponseValues.TryGetValue("code", out code))
            {
                authenticationResult = await this.authenticationContextWrapper.AcquireTokenByAuthorizationCodeAsync(
                    code,
                    redirectUri,
                    clientAssertionCertificate,
                    serviceResourceId).ConfigureAwait(false);
            }

            return(authenticationResult);
        }
        public static void ThrowIfError(IDictionary <string, string> responseValues)
        {
            if (responseValues != null)
            {
                string error            = null;
                string errorDescription = null;

                if (responseValues.TryGetValue(OAuthConstants.ErrorDescriptionKeyName, out errorDescription) ||
                    responseValues.TryGetValue(OAuthConstants.ErrorKeyName, out error))
                {
                    OAuthErrorHandler.ParseAuthenticationError(error, errorDescription);
                }
            }
        }