2 // Nonce.cs: Handles WS-Security Nonce
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System
.Security
.Cryptography
;
13 using Microsoft
.Web
.Services
;
15 using Microsoft
.Web
.Services
.Xml
;
18 namespace Microsoft
.Web
.Services
.Security
{
21 // a. Web Services Security Addendum, Version 1.0, August 18, 2002
22 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnglobspec/html/ws-security.asp
23 // b. Understanding WS-Security
24 // http://msdn.microsoft.com/webservices/building/frameworkandstudio/default.aspx?pull=/library/en-us/dnwssecur/html/understw.asp
26 public class Nonce
: IXmlElement
{
31 internal Nonce () : this (16) {}
32 internal Nonce (int size
)
34 //Compilation fix, spoulit please check the authenticity of this.
35 public Nonce () : this (16) {}
36 public Nonce (int size
)
39 nonce
= new byte [size
]; // default is 16 see reference b.
41 RandomNumberGenerator rng
= RandomNumberGenerator
.Create ();
47 internal Nonce (XmlElement element
)
49 public Nonce (XmlElement element
)
56 get { return Convert.ToBase64String (nonce); }
59 public byte[] GetValueBytes ()
61 return (byte[]) nonce
.Clone ();
64 public XmlElement
GetXml (XmlDocument document
)
67 throw new ArgumentNullException ("document");
68 // much cleaner than using StringBuilder!
69 XmlElement xel
= document
.CreateElement (WSSecurity
.Prefix
, WSSecurity
.ElementNames
.Nonce
, WSSecurity
.NamespaceURI
);
70 xel
.InnerText
= Convert
.ToBase64String (nonce
);
74 public void LoadXml (XmlElement element
)
77 throw new ArgumentNullException ("element");
78 if ((element
.LocalName
!= WSSecurity
.ElementNames
.Nonce
) || (element
.NamespaceURI
!= WSSecurity
.NamespaceURI
))
79 throw new ArgumentException ("invalid LocalName or NamespaceURI");
81 XmlAttribute xa
= element
.Attributes
[WSSecurity
.AttributeNames
.EncodingType
, WSSecurity
.NamespaceURI
];
82 if ((xa
== null) || (xa
.Value
== "Base64Binary")) {
83 nonce
= Convert
.FromBase64String (element
.InnerText
);
86 throw new NotSupportedException (xa
.Value
);