4

So I'm having a very similar issue to this post here. SOAP KeyInfo values

I am wanting to add a reference within KeyInfo but can't seem to find a way to do it through code.

Here is what the expected output should be:

<KeyInfo>
    <wsse:SecurityTokenReference>
        <wsse:Reference URI="#SecurityTest" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
    </wsse:SecurityTokenReference>
</KeyInfo>

And I do have this above where it is trying to reference from:

<wsse:BinarySecurityToken ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" 
        EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" 
        xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
        wsu:Id="SecurityTest">Base64CertStuffBlahblah
</wsse:BinarySecurityToken>

Every attempt at creating the KeyInfo portion only allows me to insert an item, like a key, to fill in this part in but I just want a reference. This code is what I've been working with but is not creating what I want at the moment.

//This creates a X509 clause but it's as far as I've got. 
//The "keyInfoData" needs to be of a different type to allow custom reference?
var signer = new SignedXmlWithId(doc) {SigningKey = Key};
KeyInfo keyInfo = new KeyInfo();
KeyInfoX509Data keyInfoData = new KeyInfoX509Data();
keyInfoData.AddCertificate(cert);
keyInfo.AddClause(keyInfoData);
signer.KeyInfo = keyInfo;

Thanks for looking, any help would be appreciated.

Community
  • 1
  • 1
Kohins
  • 327
  • 6
  • 21

1 Answers1

8

So this piece of code lets me add what I want into the KeyInfo portion.

KeyInfo keyInfo = new KeyInfo();
XmlElement x = doc.CreateElement("wsse","SecurityTokenReference", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XmlElement y = doc.CreateElement("wsse","Reference", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
y.SetAttribute("URI","#SecurityTest");
y.SetAttribute("ValueType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");
x.AppendChild(y);
var keyInfoData = new KeyInfoNode(x);
keyInfo.AddClause(keyInfoData);
signer.KeyInfo = keyInfo;

This produces the following result:

<KeyInfo>
    <wsse:SecurityTokenReference>
        <wsse:Reference URI="#SecurityTest" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" />
    </wsse:SecurityTokenReference>
</KeyInfo>

This didn't seem to fix my issue though the SOAP now "looks" correct. Maybe it will help someone else.

Kohins
  • 327
  • 6
  • 21