Beispiel #1
0
        private static async Task <AuthenticationParameters> CreateFromResourceUrlCommonAsync(Uri resourceUrl)
        {
            if (resourceUrl == null)
            {
                throw new ArgumentNullException("resourceUrl");
            }

            AuthenticationParameters authParams;

            try
            {
                IHttpClient request = new HttpClientWrapper(resourceUrl.AbsoluteUri, null);
                using (await request.GetResponseAsync().ConfigureAwait(false))
                {
                    var ex = new AdalException(AdalError.UnauthorizedResponseExpected);
                    CallState.Default.Logger.Error(null, ex);
                    throw ex;
                }
            }
            catch (HttpRequestWrapperException ex)
            {
                IHttpWebResponse response = ex.WebResponse;
                if (response == null)
                {
                    var serviceEx = new AdalServiceException(AdalErrorMessage.UnauthorizedHttpStatusCodeExpected, ex);
                    CallState.Default.Logger.Error(null, serviceEx);
                    throw serviceEx;
                }

                authParams = CreateFromUnauthorizedResponseCommon(response);
            }

            return(authParams);
        }
        public static async Task <WsTrustResponse> SendRequestAsync(WsTrustAddress wsTrustAddress, UserCredential credential, CallState callState, string cloudAudience)
        {
            IHttpClient request = new HttpClientWrapper(wsTrustAddress.Uri.AbsoluteUri, callState);

            request.ContentType = "application/soap+xml";
            if (credential.UserAuthType == UserAuthType.IntegratedAuth)
            {
                SetKerberosOption(request);
            }

            if (string.IsNullOrEmpty(cloudAudience))
            {
                cloudAudience = defaultAppliesTo;
            }

            StringBuilder messageBuilder = BuildMessage(cloudAudience, wsTrustAddress, credential);
            string        soapAction     = XmlNamespace.Issue.ToString();

            if (wsTrustAddress.Version == WsTrustVersion.WsTrust2005)
            {
                soapAction = XmlNamespace.Issue2005.ToString();
            }

            WsTrustResponse wstResponse;

            try
            {
                request.BodyParameters        = new StringRequestParameters(messageBuilder);
                request.Headers["SOAPAction"] = soapAction;
                IHttpWebResponse response = await request.GetResponseAsync().ConfigureAwait(false);

                wstResponse = WsTrustResponse.CreateFromResponse(EncodingHelper.GenerateStreamFromString(response.ResponseString), wsTrustAddress.Version);
            }
            catch (HttpRequestWrapperException ex)
            {
                string errorMessage;

                try
                {
                    using (Stream stream = EncodingHelper.GenerateStreamFromString(ex.WebResponse.ResponseString))
                    {
                        XDocument responseDocument = WsTrustResponse.ReadDocumentFromResponse(stream);
                        errorMessage = WsTrustResponse.ReadErrorResponse(responseDocument, callState);
                    }
                }
                catch (AdalException)
                {
                    errorMessage = "See inner exception for detail.";
                }

                throw new AdalServiceException(
                          AdalError.FederatedServiceReturnedError,
                          string.Format(CultureInfo.CurrentCulture, AdalErrorMessage.FederatedServiceReturnedErrorTemplate, wsTrustAddress.Uri, errorMessage),
                          null,
                          ex);
            }

            return(wstResponse);
        }
        internal static async Task <XDocument> FetchMexAsync(string federationMetadataUrl, CallState callState)
        {
            XDocument mexDocument;

            try
            {
                IHttpClient request = new HttpClientWrapper(federationMetadataUrl, callState);
                using (var response = await request.GetResponseAsync().ConfigureAwait(false))
                {
                    mexDocument = XDocument.Load(EncodingHelper.GenerateStreamFromString(response.ResponseString), LoadOptions.None);
                }
            }
            catch (HttpRequestWrapperException ex)
            {
                throw new AdalServiceException(AdalError.AccessingWsMetadataExchangeFailed, ex);
            }
            catch (XmlException ex)
            {
                throw new AdalException(AdalError.ParsingWsMetadataExchangeFailed, ex);
            }

            return(mexDocument);
        }
        public async Task TimeoutTest()
        {
            const string TestServiceUrl = "http://localhost:8080";
            using (WebApp.Start<TestService>(TestServiceUrl))
            {
                HttpClientWrapper webClient = new HttpClientWrapper(TestServiceUrl + "?delay=0&response_code=200", null) { TimeoutInMilliSeconds = 10000 };
                await webClient.GetResponseAsync();

                webClient = new HttpClientWrapper(TestServiceUrl + "?delay=0&response_code=200", null) { TimeoutInMilliSeconds = 10000 };
                await webClient.GetResponseAsync();

                try
                {
                    webClient = new HttpClientWrapper(TestServiceUrl + "?delay=0&response_code=400", null) { TimeoutInMilliSeconds = 10000 };
                    await webClient.GetResponseAsync();
                }
                catch (HttpRequestWrapperException ex)
                {
                    Verify.AreEqual(ex.WebResponse.StatusCode, HttpStatusCode.BadRequest);
                }


                try
                {
                    webClient = new HttpClientWrapper(TestServiceUrl + "?delay=10000&response_code=200", null) { TimeoutInMilliSeconds = 500 };
                    await webClient.GetResponseAsync();
                }
                catch (HttpRequestWrapperException ex)
                {
                    Verify.IsTrue(ex.InnerException is TaskCanceledException);
                    var serviceException = new AdalServiceException(AdalError.Unknown, ex);
                    Verify.AreEqual(serviceException.StatusCode, (int)HttpStatusCode.RequestTimeout);
                }
            }
        }
        [Ignore]    // This test requires TestService to run in a non-WinRT app. The code can be found in tests\Test.ADAL.NET.Unit\UnitTests.cs
        public async Task TimeoutTest()
        {
            const string TestServiceUrl = "http://localhost:8080";
            HttpClientWrapper webRequest = new HttpClientWrapper(TestServiceUrl + "?delay=0&response_code=200", null) { TimeoutInMilliSeconds = 10000 };
            await webRequest.GetResponseAsync();

            try
            {
                webRequest = new HttpClientWrapper(TestServiceUrl + "?delay=0&response_code=400", null) { TimeoutInMilliSeconds = 10000 };
                await webRequest.GetResponseAsync();
            }
            catch (WebException ex)
            {
                Verify.AreEqual((int)(ex.Status), 7);   // ProtocolError
            }

            try
            {
                webRequest = new HttpClientWrapper(TestServiceUrl + "?delay=10000&response_code=200", null) { TimeoutInMilliSeconds = 500 };
                await webRequest.GetResponseAsync();
            }
            catch (WebException ex)
            {
                Verify.AreEqual((int)(ex.Status), 6);   // RequestCanceled
            }
        }