XML 的 Saml2Assertion 为空

Saml2Assertion to XML is empty

我尝试使用 SOAP 数据创建一个 HttpWebRequest,在 SOAP header 中,我必须添加一个 SAML 2 断言,其中包含来自 Windows 证书存储的证书。

所以我是这样断言的:

Saml2Assertion oSAMLAssertion =
  new Saml2Assertion (
    new Saml2NameIdentifier (oPSCertificate.Subject)
  );
oSAMLAssertion.Id = new Saml2Id ();
oSAMLAssertion.IssueInstant = DateTime.Now;

oSAMLAssertion.Subject =
  new Saml2Subject (new Saml2NameIdentifier ("CPS") { Value = sPSID });

oSAMLAssertion.Statements
  .Add (
    new Saml2AttributeStatement (
      new Saml2Attribute ("identifiantFacturation", sIDFact)
    ));
oSAMLAssertion.Statements
  .Add (
    new Saml2AttributeStatement (
      new Saml2Attribute ("codeSpecialiteAMO", sCodeSpec)
    ));

oSAMLAssertion.SigningCredentials =
  new X509SigningCredentials (oPSCertificate);

没问题。但是,当我希望将此断言的 XML 添加到我的 SOAP 请求时,我会这样做:

StringBuilder oXML = new StringBuilder ();
XmlWriter oXMLSerializer = XmlWriter.Create (
  oXML,
  new XmlWriterSettings () { ConformanceLevel = ConformanceLevel.Fragment }
);
Saml2SecurityToken o = new Saml2SecurityToken (oSAMLAssertion);
o2.WriteToken (oXMLSerializer, o);

但是我的oXML.ToStringreturn是空的,我没有XML... 我检查了 .NET 源参考,看看我是否遗漏了什么,但似乎没有,我应该 XML ...

我做错了什么?

尝试在 xml 编写器之间添加一个字符串编写器包装器。改变这个:

XmlWriter oXMLSerializer = XmlWriter.Create(oXML, new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Fragment });

至:

using (StringWriter stringWriter = new StringWriter(oXML))
using (XmlWriter oXMLSerializer = XmlWriter.Create(stringWriter, new XmlWriterSettings() { OmitXmlDeclaration = true, Encoding = Encoding.UTF8 }))
{
    Saml2SecurityToken o = new Saml2SecurityToken(oSAMLAssertion);
    o2.WriteToken(oXMLSerializer, o);
}

要获得 XML 输出,Saml2Assertion 需要有 signedcredenitials,否则 xml 将为空白。

X509Certificate2 cert =null;
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

store.Open(OpenFlags.ReadOnly);

X509Certificate2 cert =null;

foreach (var certificate in store.Certificates)
{
    if (certificate.Thumbprint=="ffffff")
        { cert = certificate; }
}

store.Close();

var assertion = new Saml2Assertion(new Saml2NameIdentifier("Name"))
    .SigningCredentials = new X509SigningCredentials(cert);


StringBuilder sb = new StringBuilder();
var settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = false;
settings.Encoding = Encoding.UTF8;

var stringWriter = new StringWriter(sb);
var responseWriter = XmlWriter.Create(stringWriter, settings); 

new Saml2Serializer().WriteSaml2Assertion(responseWriter, assertion);

public class Saml2Serializer : Saml2SecurityTokenHandler
{
    public Saml2Serializer()
    {
        Configuration = new SecurityTokenHandlerConfiguration()
        {};
    }

public void WriteSaml2Assertion(XmlWriter writer, Saml2Assertion data)
{
    try
    { base.WriteAssertion(writer, data); }
    catch (Exception e)
    { System.Console.Write(e.StackTrace); }      
}

    public void WriteSaml2Token(XmlWriter writer, Saml2SecurityToken data)
    {
        try
        { base.WriteToken(writer, data); }
        catch (Exception e)
        { System.Console.Write(e.StackTrace); }
    }
}