Пример #1
0
        /// <summary>
        /// Calls live.com authorization server with the <see cref="OAuthRequestParameters"/> passed in, deserializes the response and returns back OAuth tokens.
        /// </summary>
        /// <param name="oAuthParameters">OAuth parameters for authorization server call</param>
        /// <exception cref="OAuthTokenRequestException">Thrown if tokens can't be received</exception>
        /// <returns>OAuth tokens</returns>
        public async Task <OAuthTokens> GetAccessTokensAsync(OAuthRequestParameters oAuthParameters, bool requireLiveConnect, string tenant, List <KeyValuePair <string, string> > additionalParams)
        {
            OAuthEndpointType endpointType = GetOAuthEndpointType(Environment, requireLiveConnect);

            var values = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("client_id", oAuthParameters.ClientId),
                new KeyValuePair <string, string>("grant_type", oAuthParameters.GrantType),
                new KeyValuePair <string, string>(oAuthParameters.GrantParamName, oAuthParameters.GrantValue),
                new KeyValuePair <string, string>("scope", EndpointUrls[endpointType].Scope)
            };

            if (oAuthParameters.RedirectUri != null)
            {
                values.Add(new KeyValuePair <string, string>("redirect_uri", oAuthParameters.RedirectUri.ToString()));
            }

            if (!string.IsNullOrEmpty(oAuthParameters.ClientSecret))
            {
                values.Add(new KeyValuePair <string, string>("client_secret", oAuthParameters.ClientSecret));
            }

            if (additionalParams != null)
            {
                values.AddRange(additionalParams);
            }

            var OAuthTokenUrl = EndpointUrls[endpointType].OAuthTokenUrl;

            if (endpointType == OAuthEndpointType.ProductionMSIdentityV2 && !(string.IsNullOrEmpty(tenant)))
            {
                OAuthTokenUrl = OAuthTokenUrl.Replace("common", tenant);
            }

            var response = await _httpService.PostAsync(new Uri(OAuthTokenUrl), values, TimeSpan.FromSeconds(100)).ConfigureAwait(false);


            var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var fragmentsSer = new DataContractJsonSerializer(typeof(Dictionary <string, string>), new DataContractJsonSerializerSettings()
                {
                    UseSimpleDictionaryFormat = true
                });

                var fragments = (Dictionary <string, string>)fragmentsSer.ReadObject(stream);
                return(new OAuthTokens(fragments["access_token"], Convert.ToInt32(fragments["expires_in"]), fragments["refresh_token"], fragments));
            }
            else
            {
                var serializer = new DataContractJsonSerializer(typeof(OAuthErrorDetailsContract));

                var errorDetailsContract = (OAuthErrorDetailsContract)serializer.ReadObject(stream);

                throw new OAuthTokenRequestException(ErrorMessages.OAuthError, new OAuthErrorDetails {
                    Description = errorDetailsContract.Description, Error = errorDetailsContract.Error
                });
            }
        }
Пример #2
0
        public Uri RedirectionUri(bool requireLiveConnect)
        {
            OAuthEndpointType endpointType = GetOAuthEndpointType(Environment, requireLiveConnect);
            var redirectUrl = EndpointUrls[endpointType].RedirectUrl;

            return(new Uri(redirectUrl));
        }
        public static Uri GetAuthorizationEndpoint(OAuthUrlParameters parameters, ApiEnvironment env, bool requireLiveConnect)
        {
            OAuthEndpointType endpointType = GetOAuthEndpointType(env, requireLiveConnect);

            return(new Uri(string.Format(
                               EndpointUrls[endpointType].AuthorizationEndpointUrl,
                               parameters.ClientId,
                               parameters.ResponseType,
                               parameters.RedirectUri) + (string.IsNullOrEmpty(parameters.State) ? "" : string.Format("&state={0}", parameters.State))));
        }
Пример #4
0
        public static Uri GetAuthorizationEndpoint(OAuthUrlParameters parameters, ApiEnvironment env, bool requireLiveConnect, string tenant)
        {
            OAuthEndpointType endpointType = GetOAuthEndpointType(env, requireLiveConnect);

            var authorizationEndpointUrl = EndpointUrls[endpointType].AuthorizationEndpointUrl;

            if (endpointType == OAuthEndpointType.ProductionMSIdentityV2 && !(string.IsNullOrEmpty(tenant)))
            {
                authorizationEndpointUrl = authorizationEndpointUrl.Replace("common", tenant);
            }
            return(new Uri(string.Format(
                               authorizationEndpointUrl,
                               parameters.ClientId,
                               parameters.ResponseType,
                               parameters.RedirectUri) + (string.IsNullOrEmpty(parameters.State) ? "" : string.Format("&state={0}", parameters.State))));
        }