Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        internal static WsTrustResponse CreateFromResponseDocument(XDocument responseDocument, WsTrustVersion version)
        {
            Dictionary <string, string> tokenResponseDictionary = new Dictionary <string, string>();

            try
            {
                XNamespace t = XmlNamespace.Trust;
                if (version == WsTrustVersion.WsTrust2005)
                {
                    t = XmlNamespace.Trust2005;
                }

                bool parseResponse = true;
                if (version == WsTrustVersion.WsTrust13)
                {
                    XElement requestSecurityTokenResponseCollection =
                        responseDocument.Descendants(t + "RequestSecurityTokenResponseCollection").FirstOrDefault();

                    if (requestSecurityTokenResponseCollection == null)
                    {
                        parseResponse = false;
                    }
                }

                if (!parseResponse)
                {
                    throw new AdalException(AdalError.ParsingWsTrustResponseFailed);
                }

                IEnumerable <XElement> tokenResponses =
                    responseDocument.Descendants(t + "RequestSecurityTokenResponse");
                foreach (var tokenResponse in tokenResponses)
                {
                    XElement tokenTypeElement = tokenResponse.Elements(t + "TokenType").FirstOrDefault();
                    if (tokenTypeElement == null)
                    {
                        continue;
                    }

                    XElement requestedSecurityToken =
                        tokenResponse.Elements(t + "RequestedSecurityToken").FirstOrDefault();
                    if (requestedSecurityToken == null)
                    {
                        continue;
                    }

                    var token = new System.Text.StringBuilder();
                    foreach (var node in requestedSecurityToken.Nodes())
                    {
                        // Since we moved from XDocument.Load(..., LoadOptions.None) to Load(..., LoadOptions.PreserveWhitespace),
                        // requestedSecurityToken can contain multiple nodes, and the first node is possibly just whitespaces e.g. "\n   ",
                        // so we concatenate all the sub-nodes to include everything
                        token.Append(node.ToString(SaveOptions.DisableFormatting));
                    }

                    tokenResponseDictionary.Add(tokenTypeElement.Value, token.ToString());
                }
            }
            catch (XmlException ex)
            {
                throw new AdalException(AdalError.ParsingWsTrustResponseFailed, ex);
            }

            if (tokenResponseDictionary.Count == 0)
            {
                throw new AdalException(AdalError.ParsingWsTrustResponseFailed);
            }

            string tokenType = tokenResponseDictionary.ContainsKey(Saml1Assertion)
                ? Saml1Assertion
                : tokenResponseDictionary.Keys.First();

            WsTrustResponse wsTrustResponse = new WsTrustResponse
            {
                TokenType = tokenType,
                Token     = tokenResponseDictionary[tokenType]
            };

            return(wsTrustResponse);
        }
        internal static WsTrustResponse CreateFromResponseDocument(XDocument responseDocument, WsTrustVersion version)
        {
            Dictionary <string, string> tokenResponseDictionary = new Dictionary <string, string>();

            try
            {
                XNamespace t = XmlNamespace.Trust;
                if (version == WsTrustVersion.WsTrust2005)
                {
                    t = XmlNamespace.Trust2005;
                }

                bool parseResponse = true;
                if (version == WsTrustVersion.WsTrust13)
                {
                    XElement requestSecurityTokenResponseCollection =
                        responseDocument.Descendants(t + "RequestSecurityTokenResponseCollection").FirstOrDefault();

                    if (requestSecurityTokenResponseCollection == null)
                    {
                        parseResponse = false;
                    }
                }

                if (!parseResponse)
                {
                    throw new AdalException(AdalError.ParsingWsTrustResponseFailed);
                }

                IEnumerable <XElement> tokenResponses =
                    responseDocument.Descendants(t + "RequestSecurityTokenResponse");
                foreach (var tokenResponse in tokenResponses)
                {
                    XElement tokenTypeElement = tokenResponse.Elements(t + "TokenType").FirstOrDefault();
                    if (tokenTypeElement == null)
                    {
                        continue;
                    }

                    XElement requestedSecurityToken =
                        tokenResponse.Elements(t + "RequestedSecurityToken").FirstOrDefault();
                    if (requestedSecurityToken == null)
                    {
                        continue;
                    }

                    tokenResponseDictionary.Add(tokenTypeElement.Value,
                                                requestedSecurityToken.FirstNode.ToString(SaveOptions.DisableFormatting));
                }
            }
            catch (XmlException ex)
            {
                throw new AdalException(AdalError.ParsingWsTrustResponseFailed, ex);
            }

            if (tokenResponseDictionary.Count == 0)
            {
                throw new AdalException(AdalError.ParsingWsTrustResponseFailed);
            }

            string tokenType = tokenResponseDictionary.ContainsKey(Saml1Assertion)
                ? Saml1Assertion
                : tokenResponseDictionary.Keys.First();

            WsTrustResponse wsTrustResponse = new WsTrustResponse
            {
                TokenType = tokenType,
                Token     = tokenResponseDictionary[tokenType]
            };

            return(wsTrustResponse);
        }