Esempio n. 1
0
        private Uri GetAccountApiEndpoint(OAuthDataServiceAdapter dataServiceAdapter, ServiceVersionAdapter versionAdapter, Uri apiServer, UserAgentAdapter userAgentAdapter, ClientRequestIdAdapter clientRequestIdAdapter)
        {
            MediaRetryPolicy retryPolicy = new MediaRetryPolicy(
                GetWebRequestTransientErrorDetectionStrategy(),
                RetryStrategyFactory.DefaultStrategy());

            Uri apiEndpoint = null;

            retryPolicy.ExecuteAction(
                () =>
            {
                HttpWebRequest request    = (HttpWebRequest)WebRequest.Create(apiServer);
                request.AllowAutoRedirect = false;
                dataServiceAdapter.AddAccessTokenToRequest(request);
                versionAdapter.AddVersionToRequest(request);
                userAgentAdapter.AddUserAgentToRequest(request);
                clientRequestIdAdapter.AddClientRequestId(request);

                using (WebResponse response = request.GetResponse())
                {
                    apiEndpoint = GetAccountApiEndpointFromResponse(response);
                }
            }
                );

            return(apiEndpoint);
        }
 private void SetMediaServiceCredentials(string clientId, string clientSecret, string scope, string acsBaseAddress)
 {
     this.ClientId     = clientId;
     this.ClientSecret = clientSecret;
     this.Scope        = scope;
     if (RefreshTokenRetryPolicy == null)
     {
         this.RefreshTokenRetryPolicy = new RetryPolicy(new WebRequestTransientErrorDetectionStrategy(),
                                                        RetryStrategyFactory.DefaultStrategy());
     }
     this._random = new Random();
 }
 public AcsTokenProvider(string clientId, string clientSecret, string scope, IList <string> acsBaseAddressList)
 {
     ValidateStringArgumentIsNotNullOrEmpty(clientId, "clientId");
     ValidateStringArgumentIsNotNullOrEmpty(clientSecret, "clientSecret");
     ValidateStringArgumentIsNotNullOrEmpty(scope, "scope");
     ClientId                = clientId;
     ClientSecret            = clientSecret;
     Scope                   = scope;
     _acsBaseAddressList     = new List <string>(acsBaseAddressList);
     RefreshTokenRetryPolicy = new RetryPolicy(new WebRequestTransientErrorDetectionStrategy(),
                                               RetryStrategyFactory.DefaultStrategy());
 }
Esempio n. 4
0
        /// <summary>
        /// Requests ACS token from the server and stores it for future use.
        /// </summary>
        public void RefreshToken()
        {
            using (WebClient client = new WebClient())
            {
                client.BaseAddress = this.AcsBaseAddress;

                var oauthRequestValues = new NameValueCollection
                {
                    { "grant_type", GrantType },
                    { "client_id", this.ClientId },
                    { "client_secret", this.ClientSecret },
                    { "scope", this.Scope },
                };

                RetryPolicy retryPolicy = new RetryPolicy(new WebRequestTransientErrorDetectionStrategy(), RetryStrategyFactory.DefaultStrategy());

                retryPolicy.ExecuteAction(
                    () =>
                {
                    byte[] responseBytes = client.UploadValues("/v2/OAuth2-13", "POST", oauthRequestValues);
                    SetAcsToken(responseBytes);
                });
            }
        }