| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- //
- // BootstrapContext.cs
- //
- // Author:
- // Robert J. van der Boon ([email protected])
- //
- // Copyright (C) 2014 Robert J. van der Boon
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_4_5
- using System;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Xml;
- namespace System.IdentityModel.Tokens {
- [Serializable]
- public class BootstrapContext : ISerializable {
- /// <summary>Gets the string that was used to initialize the context.</summary>
- public string Token { get; private set; }
- /// <summary>Gets the array that was used to initialize the context.</summary>
- public byte [] TokenBytes { get; private set; }
- /// <summary>Gets the security token that was used to initialize the context.</summary>
- public SecurityToken SecurityToken { get; private set; }
- /// <summary>Gets the token handler that was used to initialize the context.</summary>
- public SecurityTokenHandler SecurityTokenHandler { get; private set; }
- /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified string.</summary>
- public BootstrapContext (string token)
- {
- if (token == null)
- throw new ArgumentNullException ("token");
- Token = token;
- }
- /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified array.</summary>
- public BootstrapContext (byte [] token)
- {
- if (token == null)
- throw new ArgumentNullException ("token");
- TokenBytes = token;
- }
- /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified security token and token handler.</summary>
- public BootstrapContext (SecurityToken token, SecurityTokenHandler handler)
- {
- if (token == null)
- throw new ArgumentNullException ("token");
- if (handler == null)
- throw new ArgumentNullException ("handler");
- SecurityToken = token;
- SecurityTokenHandler = handler;
- }
- /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class from a stream.</summary>
- protected BootstrapContext (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
- char type = info.GetChar ("K");
- switch (type) {
- case 'S':
- Token = info.GetString ("T");
- break;
- case 'B':
- TokenBytes = (byte [])info.GetValue ("T", typeof (byte []));
- break;
- case 'T':
- Token = Encoding.UTF8.GetString (Convert.FromBase64String (info.GetString ("T")));
- break;
- }
- }
- /// <summary>Populates the <see cref="SerializationInfo"/> with data needed to serialize the current <see cref="BootstrapContext"/> object.</summary>
- public void GetObjectData (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
- if (Token != null) {
- info.AddValue ("K", 'S');
- info.AddValue ("T", Token);
- }
- else if (TokenBytes != null) {
- info.AddValue ("K", 'B');
- info.AddValue ("T", TokenBytes);
- }
- else if (SecurityToken != null && SecurityTokenHandler != null) {
- info.AddValue ("K", 'T');
- using (var ms = new MemoryStream ())
- using (var streamWriter = new StreamWriter (ms, new UTF8Encoding (false)))
- using (var writer = XmlWriter.Create (streamWriter, new XmlWriterSettings { OmitXmlDeclaration = true })) {
- SecurityTokenHandler.WriteToken (writer, SecurityToken);
- writer.Flush ();
- info.AddValue ("T", Convert.ToBase64String (ms.ToArray ()));
- }
- }
- }
- }
- }
- #endif
|