示例#1
0
        private T ExecuteRESTRequest <T>(CloudIdentity identity, Uri absoluteUri, HttpMethod method, object body, Dictionary <string, string> queryStringParameter, Dictionary <string, string> headers, bool isRetry, JsonRequestSettings requestSettings,
                                         Func <Uri, HttpMethod, string, Dictionary <string, string>, Dictionary <string, string>, JsonRequestSettings, T> callback) where T : Response
        {
            identity = GetDefaultIdentity(identity);

            if (requestSettings == null)
            {
                requestSettings = BuildDefaultRequestSettings();
            }

            if (headers == null)
            {
                headers = new Dictionary <string, string>();
            }

            headers["X-Auth-Token"] = IdentityProvider.GetToken(identity, isRetry);

            string bodyStr = null;

            if (body != null)
            {
                if (body is JObject)
                {
                    bodyStr = body.ToString();
                }
                else if (body is string)
                {
                    bodyStr = body as string;
                }
                else
                {
                    bodyStr = JsonConvert.SerializeObject(body, new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    });
                }
            }

            if (string.IsNullOrWhiteSpace(requestSettings.UserAgent))
            {
                requestSettings.UserAgent = GetUserAgentHeaderValue();
            }

            var response = callback(absoluteUri, method, bodyStr, headers, queryStringParameter, requestSettings);

            // on errors try again 1 time.
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                if (!isRetry)
                {
                    return(ExecuteRESTRequest <T>(identity, absoluteUri, method, body, queryStringParameter, headers, true, requestSettings, callback));
                }
            }

            CheckResponse(response);

            return(response);
        }
示例#2
0
        public void TestGetToken()
        {
            IIdentityProvider provider = Bootstrapper.CreateIdentityProvider();

            IdentityToken token = provider.GetToken();

            Assert.IsNotNull(token);

            // ensure the provider is caching the access token
            IdentityToken cachedToken = provider.GetToken();

            Assert.AreSame(token, cachedToken);

            // ensure that the provider refreshes the token upon request
            IdentityToken newToken = provider.GetToken(forceCacheRefresh: true);

            Assert.AreNotSame(token, newToken);

            // ensure the the refresh was applied to the cache
            IdentityToken newCachedToken = provider.GetToken();

            Assert.AreSame(newToken, newCachedToken);
        }