BootstrapContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // BootstrapContext.cs
  3. //
  4. // Author:
  5. // Robert J. van der Boon ([email protected])
  6. //
  7. // Copyright (C) 2014 Robert J. van der Boon
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. #if NET_4_5
  29. using System;
  30. using System.IO;
  31. using System.Runtime.Serialization;
  32. using System.Text;
  33. using System.Xml;
  34. namespace System.IdentityModel.Tokens {
  35. [Serializable]
  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; }
  45. /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified string.</summary>
  46. public BootstrapContext (string token)
  47. {
  48. if (token == null)
  49. throw new ArgumentNullException ("token");
  50. Token = token;
  51. }
  52. /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified array.</summary>
  53. public BootstrapContext (byte [] token)
  54. {
  55. if (token == null)
  56. throw new ArgumentNullException ("token");
  57. TokenBytes = token;
  58. }
  59. /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified security token and token handler.</summary>
  60. public BootstrapContext (SecurityToken token, SecurityTokenHandler handler)
  61. {
  62. if (token == null)
  63. throw new ArgumentNullException ("token");
  64. if (handler == null)
  65. throw new ArgumentNullException ("handler");
  66. SecurityToken = token;
  67. SecurityTokenHandler = handler;
  68. }
  69. /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class from a stream.</summary>
  70. protected BootstrapContext (SerializationInfo info, StreamingContext context)
  71. {
  72. if (info == null)
  73. throw new ArgumentNullException ("info");
  74. char type = info.GetChar ("K");
  75. switch (type) {
  76. case 'S':
  77. Token = info.GetString ("T");
  78. break;
  79. case 'B':
  80. TokenBytes = (byte [])info.GetValue ("T", typeof (byte []));
  81. break;
  82. case 'T':
  83. Token = Encoding.UTF8.GetString (Convert.FromBase64String (info.GetString ("T")));
  84. break;
  85. }
  86. }
  87. /// <summary>Populates the <see cref="SerializationInfo"/> with data needed to serialize the current <see cref="BootstrapContext"/> object.</summary>
  88. public void GetObjectData (SerializationInfo info, StreamingContext context)
  89. {
  90. if (info == null)
  91. throw new ArgumentNullException ("info");
  92. if (Token != null) {
  93. info.AddValue ("K", 'S');
  94. info.AddValue ("T", Token);
  95. }
  96. else if (TokenBytes != null) {
  97. info.AddValue ("K", 'B');
  98. info.AddValue ("T", TokenBytes);
  99. }
  100. else if (SecurityToken != null && SecurityTokenHandler != null) {
  101. info.AddValue ("K", 'T');
  102. using (var ms = new MemoryStream ())
  103. using (var streamWriter = new StreamWriter (ms, new UTF8Encoding (false)))
  104. using (var writer = XmlWriter.Create (streamWriter, new XmlWriterSettings { OmitXmlDeclaration = true })) {
  105. SecurityTokenHandler.WriteToken (writer, SecurityToken);
  106. writer.Flush ();
  107. info.AddValue ("T", Convert.ToBase64String (ms.ToArray ()));
  108. }
  109. }
  110. }
  111. }
  112. }
  113. #endif