5 // Robert J. van der Boon (rjvdboon@gmail.com)
7 // Copyright (C) 2014 Robert J. van der Boon
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.Serialization;
34 namespace System.IdentityModel.Tokens {
36 public class BootstrapContext : ISerializable {
37 /// <summary>Gets the string that was used to initialize the context.</summary>
38 public string Token { get; private set; }
39 /// <summary>Gets the array that was used to initialize the context.</summary>
40 public byte [] TokenBytes { get; private set; }
41 /// <summary>Gets the security token that was used to initialize the context.</summary>
42 public SecurityToken SecurityToken { get; private set; }
43 /// <summary>Gets the token handler that was used to initialize the context.</summary>
44 public SecurityTokenHandler SecurityTokenHandler { get; private set; }
46 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified string.</summary>
47 public BootstrapContext (string token)
50 throw new ArgumentNullException ("token");
54 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified array.</summary>
55 public BootstrapContext (byte [] token)
58 throw new ArgumentNullException ("token");
62 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified security token and token handler.</summary>
63 public BootstrapContext (SecurityToken token, SecurityTokenHandler handler)
66 throw new ArgumentNullException ("token");
68 throw new ArgumentNullException ("handler");
69 SecurityToken = token;
70 SecurityTokenHandler = handler;
73 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class from a stream.</summary>
74 protected BootstrapContext (SerializationInfo info, StreamingContext context)
77 throw new ArgumentNullException ("info");
78 char type = info.GetChar ("K");
81 Token = info.GetString ("T");
84 TokenBytes = (byte [])info.GetValue ("T", typeof (byte []));
87 Token = Encoding.UTF8.GetString (Convert.FromBase64String (info.GetString ("T")));
92 /// <summary>Populates the <see cref="SerializationInfo"/> with data needed to serialize the current <see cref="BootstrapContext"/> object.</summary>
93 public void GetObjectData (SerializationInfo info, StreamingContext context)
96 throw new ArgumentNullException ("info");
98 info.AddValue ("K", 'S');
99 info.AddValue ("T", Token);
100 } else if (TokenBytes != null) {
101 info.AddValue ("K", 'B');
102 info.AddValue ("T", TokenBytes);
103 } else if (SecurityToken != null && SecurityTokenHandler != null) {
104 info.AddValue ("K", 'T');
105 using (var ms = new MemoryStream ())
106 using (var streamWriter = new StreamWriter (ms, new UTF8Encoding (false)))
107 using (var writer = XmlWriter.Create (streamWriter, new XmlWriterSettings { OmitXmlDeclaration = true })) {
108 SecurityTokenHandler.WriteToken (writer, SecurityToken);
110 info.AddValue ("T", Convert.ToBase64String (ms.ToArray ()));