2015-04-22 98 views
0

我正在開發解決方案deserializeSAMLxml。但是,面對一個錯誤說<Issuer xmlns ='urn:oasis:names:tc:SAML:2.0:assertion'> was not expected

「發行人的xmlns = '金塔:綠洲:名稱:TC:SAML:2.0:斷言' 沒有 預期」

代碼

 XmlNamespaceManager ns = new XmlNamespaceManager(SAMLXML.NameTable); 
     ns.AddNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion"); 

     XmlElement xeAssertion = SAMLXML.DocumentElement.SelectSingleNode("saml:Assertion",ns) as XmlElement; 

     AssertionType assertionType = new AssertionType(); 
     XmlSerializer serializer = new XmlSerializer(assertionType.GetType(),ns.DefaultNamespace); 

     MemoryStream ms = new MemoryStream(Encoding.UTF32.GetBytes(xeAssertion.InnerXml.ToString())); 
     ms.Position = 0; 
     ms.Seek(0, SeekOrigin.Begin); 

     AssertionType assertion = (AssertionType)serializer.Deserialize(ms); 

     return assertion; 
+0

是'從某些庫AssertionType'?或者是你自己的班級?看起來它不處理''元素。 –

+0

這是一個基於SAML XML響應的XSD.exe生成的類。點擊https://rnd.feide.no/2007/12/10/example_saml_2_0_request_and_response/舉例 – abn

+0

您可以粘貼SAMLXML的內容 –

回答

0

終於找到了解決辦法

代碼

  XmlNamespaceManager ns = new XmlNamespaceManager(SAMLXML.NameTable); 
      ns.AddNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion"); 

      XmlElement xeAssertion = SAMLXML.DocumentElement.SelectSingleNode("saml:Assertion", ns) as XmlElement; 


      string xml = null; 
      var memoryStream = new MemoryStream(); 
      var serializer = new XmlSerializer(xeAssertion.GetType()); 
      var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8); 
      serializer.Serialize(streamWriter, xeAssertion); 
      memoryStream = (MemoryStream)streamWriter.BaseStream; 

      xml = memoryStream.ToArray().Utf8ByteArrayToString(); 


      var serializer = new XmlSerializer(typeof(AssertionType)); 
      var memoryStream = new MemoryStream(xml.StringToUtf8ByteArray()); 


      AssertionType assertion = (AssertionType)serializer.Deserialize(memoryStream); 

      return assertion; 


**SAML** 

    <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="" Version="2.0" IssueInstant="" Destination="Recipient" xmlns="urn:oasis:names:tc:SAML:2.0:protocol"> 
    <saml:Issuer>abnTest</saml:Issuer> 
    <Status> 
    <StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success" /> 
    </Status> 
    <saml:Assertion Version="2.0" ID=""> 
    <saml:Issuer></saml:Issuer> 
    <saml:Subject> 
     <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">Subject</saml:NameID> 
     <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"> 
     <saml:SubjectConfirmationData Recipient="Recipient" NotOnOrAfter="" /> 
     </saml:SubjectConfirmation> 
    </saml:Subject> 
    <saml:Conditions NotBefore="" NotOnOrAfter=""> 
     <saml:AudienceRestriction> 
     <saml:Audience>Audience</saml:Audience> 
     </saml:AudienceRestriction> 
    </saml:Conditions> 
    <saml:AuthnStatement AuthnInstant="" SessionIndex=""> 
     <saml:AuthnContext> 
     <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml:AuthnContextClassRef> 
     </saml:AuthnContext> 
    </saml:AuthnStatement> 
    <saml:AttributeStatement> 
     <saml:Attribute Name="SessionKey" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"> 
     <saml:AttributeValue xsi:type="xsd:string">{}</saml:AttributeValue> 
     </saml:Attribute>  
     <saml:Attribute Name="Username" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"> 
     <saml:AttributeValue xsi:type="xsd:string"></saml:AttributeValue> 
     </saml:Attribute>  
    </saml:AttributeStatement> 
    </saml:Assertion> 
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> 
    <SignedInfo> 
     <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /> 
     <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" /> 
     <Reference URI="#_4-90d7-cbc23ca53255"> 
     <Transforms> 
      <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /> 
      <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" /> 
     </Transforms> 
     <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" /> 
     <DigestValue>=</DigestValue> 
     </Reference> 
    </SignedInfo> 
    <SignatureValue>GVRL/+q=</SignatureValue> 
    </Signature> 
</Response> 
+1

如果這是您的實際SAML內容,您有幾個問題。我建議使用SAML的現有庫,而不是自己解析XML。 SAML響應/斷言的正確驗證有*許多細節。 –

相關問題