<issuer xmlns =“urn:oasis:names:tc:SAML:2.0:断言”>未被预期</issuer>

时间:2015-04-22 04:42:14

标签: c# xml namespaces saml assertions

我正在为deserialize SAML xml开发解决方案。但面临一个错误说

  

“Issuer xmlns ='urn:oasis:names:tc:SAML:2.0:assertion'不是   预期“

代码

        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;

1 个答案:

答案 0 :(得分: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>