コード例 #1
0
        /// <summary>
        /// Retrieves OAuth access and refresh tokens from the Microsoft Account authorization service
        /// using the specified authorization response redirect <see cref="Uri"/>.
        /// </summary>
        /// <param name="responseUri">
        /// The authorization response redirect <see cref="Uri"/> that contains the authorization code.
        /// </param>
        /// <remarks>
        /// For more information, see <see href="https://tools.ietf.org/html/rfc6749#section-4.1.2">Authorization Response section in the OAuth 2.0 spec</see>.
        /// </remarks>
        /// <returns>A task that represents the asynchronous operation. The task result will be an <see cref="OAuthTokens"/> object.</returns>
        /// <exception cref="OAuthTokenRequestException">Thrown if tokens can't be received due to an error received from the Microsoft Account authorization server.</exception>
        public async Task <OAuthTokens> RequestAccessAndRefreshTokensAsync(Uri responseUri, List <KeyValuePair <string, string> > additionalParams = null)
        {
            if (responseUri == null)
            {
                throw new ArgumentNullException("responseUri");
            }

            var queryParts = responseUri.ParseQuery();

            string error;

            if (queryParts.TryGetValue("error", out error))
            {
                var details = new OAuthErrorDetails {
                    Error = Uri.UnescapeDataString(error)
                };

                string errorDescription;

                if (queryParts.TryGetValue("error_description", out errorDescription))
                {
                    details.Description = Uri.UnescapeDataString(errorDescription);
                }

                throw new OAuthTokenRequestException(ErrorMessages.OAuthError, details);
            }

            if (!queryParts.ContainsKey("code"))
            {
                throw new ArgumentException(ErrorMessages.UriDoesntContainCode);
            }

            var code = queryParts["code"];

            OAuthTokens = await _oauthService.GetAccessTokensAsync(new OAuthRequestParameters
            {
                ClientId       = ClientId,
                ClientSecret   = _optionalClientSecret,
                RedirectUri    = RedirectionUri,
                GrantType      = "authorization_code",
                GrantParamName = "code",
                GrantValue     = code,
            },
                                                                   RequireLiveConnect,
                                                                   Tenant, additionalParams).ConfigureAwait(false);

            RaiseNewTokensReceivedEvent();

            return(OAuthTokens);
        }
コード例 #2
0
        /// <summary>
        /// Retrieves OAuth access and refresh tokens from the Microsoft Account authorization service 
        /// using the specified authorization response redirect <see cref="Uri"/>.
        /// </summary>
        /// <param name="responseUri">
        /// The authorization response redirect <see cref="Uri"/> that contains the authorization code.        
        /// </param>
        /// <remarks>
        /// For more information, see <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.1.2">Authorization Response section in the OAuth 2.0 spec</see>.
        /// </remarks>
        /// <returns>A task that represents the asynchronous operation. The task result will be an <see cref="OAuthTokens"/> object.</returns>      
        /// <exception cref="OAuthTokenRequestException">Thrown if tokens can't be received due to an error received from the Microsoft Account authorization server.</exception>  
        public async Task<OAuthTokens> RequestAccessAndRefreshTokensAsync(Uri responseUri)
        {
            if (responseUri == null)
            {
                throw new ArgumentNullException("responseUri");
            }

            var queryParts = responseUri.ParseQuery();

            string error;

            if (queryParts.TryGetValue("error", out error))
            {
                var details = new OAuthErrorDetails { Error = Uri.UnescapeDataString(error) };

                string errorDescription;

                if (queryParts.TryGetValue("error_description", out errorDescription))
                {
                    details.Description = Uri.UnescapeDataString(errorDescription);
                }

                throw new OAuthTokenRequestException(ErrorMessages.OAuthError, details);
            }

            if (!queryParts.ContainsKey("code"))
            {
                throw new ArgumentException(ErrorMessages.UriDoesntContainCode);
            }

            var code = queryParts["code"];

            OAuthTokens = await _oauthService.GetAccessTokensAsync(new OAuthRequestParameters
            {
                ClientId = ClientId,
                ClientSecret = _optionalClientSecret,
                RedirectUri = RedirectionUri,
                GrantType = "authorization_code",
                GrantParamName = "code",
                GrantValue = code
            }).ConfigureAwait(false);

            RaiseNewTokensReceivedEvent();

            return OAuthTokens;
        }