1

I am trying to generate SOAP Username token using C# but without success

  <wsse:UsernameToken wsu:Id='UsernameToken-1231231231123123'>
         <wsse:Username>UserName</wsse:Username>
          <wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>Password</wsse:Password>
                                             </wsse:UsernameToken>

the above one is the correct format for our SOAP endpoint but when i am trying to generate token using UsernameToken from namespace

Microsoft.Web.Services2.Security.Tokens

    UsernameToken t;
    t = new UsernameToken("UserName", "Password");
    string usernameTokenSection1 = t.GetXml(new XmlDocument()).OuterXml.ToString();

I got this result which is not working

    <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-cf96131b-1528-46a1-8f00-f61af616db91" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Username>Username</wsse:Username>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">+S3AH9NHRtOpgdxEwqIVIg==</wsse:Nonce><wsu:Created>2020-04-04T06:46:53Z</wsu:Created></wsse:UsernameToken>
AAHN
  • 381
  • 1
  • 4
  • 18
  • Look at the schema (xsd) of the working and non working. They are very similar but different. – jdweng Apr 04 '20 at 07:45
  • yes how i can generate wsu:Id='UsernameToken instead of wsu:Id="SecurityToken- – AAHN Apr 04 '20 at 07:49
  • @AAHN adjust your post clarify where is the issue, and what is the current results of your tries, and what is the correct results that you want to achieve, and What is wrong with `Microsoft.Web.Services2.Security.Tokens` ? – iSR5 Apr 04 '20 at 07:53
  • Not an expert on differences. Search msdn page for XML. Looks like there are 6 different token classes that generate xml : https://learn.microsoft.com/en-us/dotnet/api/system.identitymodel.tokens?view=netframework-4.8 – jdweng Apr 04 '20 at 07:56
  • @jdweng to be honest, I've got confused.after you mentioned it, I've reviewed it three times, and poor me, I guess I need new glasses ;(. I know now where is the issue (Beside my glasses). – iSR5 Apr 04 '20 at 08:02
  • @iSR5 actually my issue is how i can generate usernametoken i am working with third party and they sendme a sample request contains user wsse:UsernameToken wsu:Id='UsernameToken-1231231231123123'> but in C# Code i can't find a way to generate same things based on UserName and Password – AAHN Apr 04 '20 at 08:14

1 Answers1

1

the wsu:Id='UsernameToken-1231231231123123' attribute is Id property of UsernameToken

So, you do this :

UsernameToken t = new UsernameToken("UserName", "Password", PasswordOption.SendPlainText)
{ 
    Id = "UsernameToken-1231231231123123"                 
};

Then you can parse it in XmlDocument or XDocument which would give you the ability to adjust the elements to fit your requirements.

you can parse it like this var doc = XDocument.Parse(usernameTokenSection1);

Now, using the parsed XML, you can adjust it to your requirements. For instance you can remove Nonce and Created elements like this :

var doc = XDocument.Parse(usernameTokenSection1);

XNamespace wsu = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
XNamespace wsse = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

doc.Root.Descendants(wsse + "Nonce").Remove();
doc.Root.Descendants(wsu + "Created").Remove();
iSR5
  • 3,274
  • 2
  • 14
  • 13
  • Thank you for your comment Actually Iknow that UsernameToken-1231231231123123 is the Id but how i can generate this field automatic this is my question since when I call UsernameToken t = new UsernameToken("UserName", "Password", PasswordOption.SendPlainText); the Id is wsu:Id="SecurityToken-c291196b-7d46-499b-aaf8-fe8ba8d45d42" not usertoken – AAHN Apr 04 '20 at 09:45
  • @AAHN could you please provide an actual sample of the real `Id=UsernameToken` attribute. As I think what you've is just a custom attribute value. If you provide a an actual value, I might know better where you can generate it. – iSR5 Apr 04 '20 at 10:33
  • what i got like if i used SecurityToken-c291196b-7d46-499b-aaf8-fe8ba8d45d42" it's work fine as same as using wsu:Id='UsernameToken-c291196b7d46499baaf8fe8ba8d45d42' the issue was because of - character not working with usernameToken I don't know if you got my point but now the issue solved – AAHN Apr 04 '20 at 11:00
  • @AAHN this is the default Id value. just do this `t = new UsernameToken("UserName", "Password", PasswordOption.SendPlainText);` without specifying the Id. It will generate the SecurityToken Id automatically. – iSR5 Apr 04 '20 at 11:03