public static async Task <WsTrustResponse> SendRequestAsync(WsTrustAddress wsTrustAddress, UserCredential credential, CallState callState) { HttpClientWrapper request = new HttpClientWrapper(wsTrustAddress.Uri.AbsoluteUri, callState); request.ContentType = "application/soap+xml"; if (credential.UserAuthType == UserAuthType.IntegratedAuth) { SetKerberosOption(request); } StringBuilder messageBuilder = BuildMessage(DefaultAppliesTo, wsTrustAddress, credential); WsTrustResponse wstResponse; try { request.BodyParameters = new StringRequestParameters(messageBuilder); IHttpWebResponse response = await request.GetResponseAsync().ConfigureAwait(false); wstResponse = WsTrustResponse.CreateFromResponse(response.ResponseStream, wsTrustAddress.Version); } catch (WebException ex) { PlatformPlugin.Logger.Error(callState, ex); string errorMessage; try { XDocument responseDocument = WsTrustResponse.ReadDocumentFromResponse(ex.Response.GetResponseStream()); errorMessage = WsTrustResponse.ReadErrorResponse(responseDocument, callState); } catch (MsalException) { errorMessage = "See inner exception for detail."; } throw new MsalServiceException( MsalError.FederatedServiceReturnedError, string.Format(MsalErrorMessage.FederatedServiceReturnedErrorTemplate, wsTrustAddress.Uri, errorMessage), null, ex); } return(wstResponse); }
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) { 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; } // TODO #123622: We need to disable formatting due to a potential service bug. Remove the ToString argument when problem is fixed. tokenResponseDictionary.Add(tokenTypeElement.Value, requestedSecurityToken.FirstNode.ToString(SaveOptions.DisableFormatting)); } } } catch (XmlException ex) { PlatformPlugin.Logger.Error(null, ex); throw new MsalException(MsalError.ParsingWsTrustResponseFailed, ex); } if (tokenResponseDictionary.Count == 0) { throw new MsalException(MsalError.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) { 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; } // TODO #123622: We need to disable formatting due to a potential service bug. Remove the ToString argument when problem is fixed. tokenResponseDictionary.Add(tokenTypeElement.Value, requestedSecurityToken.FirstNode.ToString(SaveOptions.DisableFormatting)); } } } catch (XmlException ex) { PlatformPlugin.Logger.Error(null, ex); throw new MsalException(MsalError.ParsingWsTrustResponseFailed, ex); } if (tokenResponseDictionary.Count == 0) { throw new MsalException(MsalError.ParsingWsTrustResponseFailed); } string tokenType = tokenResponseDictionary.ContainsKey(Saml1Assertion) ? Saml1Assertion : tokenResponseDictionary.Keys.First(); WsTrustResponse wsTrustResponse = new WsTrustResponse { TokenType = tokenType, Token = tokenResponseDictionary[tokenType] }; return(wsTrustResponse); }