FormsAuthenticationTicket.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // System.Web.Security.FormsAuthenticationTicket
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. namespace System.Web.Security
  33. {
  34. [Serializable]
  35. public sealed class FormsAuthenticationTicket
  36. {
  37. int version;
  38. bool persistent;
  39. DateTime issue_date;
  40. DateTime expiration;
  41. string name;
  42. string cookie_path;
  43. string user_data;
  44. /*
  45. internal void ToStr ()
  46. {
  47. Console.WriteLine ("version: {0}", version);
  48. Console.WriteLine ("persistent: {0}", persistent);
  49. Console.WriteLine ("issue_date: {0}", issue_date);
  50. Console.WriteLine ("expiration: {0}", expiration);
  51. Console.WriteLine ("name: {0}", name);
  52. Console.WriteLine ("cookie_path: {0}", cookie_path);
  53. Console.WriteLine ("user_data: {0}", user_data);
  54. }
  55. */
  56. internal byte [] ToByteArray ()
  57. {
  58. MemoryStream ms = new MemoryStream ();
  59. BinaryWriter writer = new BinaryWriter (ms);
  60. writer.Write (version);
  61. writer.Write (persistent);
  62. writer.Write (issue_date.Ticks);
  63. writer.Write (expiration.Ticks);
  64. writer.Write (name != null);
  65. if (name != null)
  66. writer.Write (name);
  67. writer.Write (cookie_path != null);
  68. if (cookie_path != null)
  69. writer.Write (cookie_path);
  70. writer.Write (user_data != null);
  71. if (user_data != null)
  72. writer.Write (user_data);
  73. writer.Flush ();
  74. return ms.ToArray ();
  75. }
  76. internal static FormsAuthenticationTicket FromByteArray (byte [] bytes)
  77. {
  78. MemoryStream ms = new MemoryStream (bytes);
  79. BinaryReader reader = new BinaryReader (ms);
  80. FormsAuthenticationTicket ticket = new FormsAuthenticationTicket ();
  81. ticket.version = reader.ReadInt32 ();
  82. ticket.persistent = reader.ReadBoolean ();
  83. ticket.issue_date = new DateTime (reader.ReadInt64 ());
  84. ticket.expiration = new DateTime (reader.ReadInt64 ());
  85. if (reader.ReadBoolean ())
  86. ticket.name = reader.ReadString ();
  87. if (reader.ReadBoolean ())
  88. ticket.cookie_path = reader.ReadString ();
  89. if (reader.ReadBoolean ())
  90. ticket.user_data = reader.ReadString ();
  91. return ticket;
  92. }
  93. private FormsAuthenticationTicket ()
  94. {
  95. }
  96. public FormsAuthenticationTicket (int version,
  97. string name,
  98. DateTime issueDate,
  99. DateTime expiration,
  100. bool isPersistent,
  101. string userData)
  102. {
  103. this.version = version;
  104. this.name = name;
  105. this.issue_date = issueDate;
  106. this.expiration = expiration;
  107. this.persistent = isPersistent;
  108. this.user_data = userData;
  109. this.cookie_path = "/";
  110. }
  111. public FormsAuthenticationTicket (int version,
  112. string name,
  113. DateTime issueDate,
  114. DateTime expiration,
  115. bool isPersistent,
  116. string userData,
  117. string cookiePath)
  118. {
  119. this.version = version;
  120. this.name = name;
  121. this.issue_date = issueDate;
  122. this.expiration = expiration;
  123. this.persistent = isPersistent;
  124. this.user_data = userData;
  125. this.cookie_path = cookiePath;
  126. }
  127. public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
  128. {
  129. this.version = 1;
  130. this.name = name;
  131. this.issue_date = DateTime.Now;
  132. this.persistent = isPersistent;
  133. if (persistent)
  134. expiration = issue_date.AddYears (50);
  135. else
  136. expiration = issue_date.AddMinutes ((double) timeout);
  137. this.user_data = "";
  138. this.cookie_path = "/";
  139. }
  140. internal void SetDates (DateTime issue_date, DateTime expiration)
  141. {
  142. this.issue_date = issue_date;
  143. this.expiration = expiration;
  144. }
  145. internal FormsAuthenticationTicket Clone ()
  146. {
  147. return new FormsAuthenticationTicket (version,
  148. name,
  149. issue_date,
  150. expiration,
  151. persistent,
  152. user_data,
  153. cookie_path);
  154. }
  155. public string CookiePath {
  156. get { return cookie_path; }
  157. }
  158. public DateTime Expiration {
  159. get { return expiration; }
  160. }
  161. public bool Expired {
  162. get { return DateTime.Now > expiration; }
  163. }
  164. public bool IsPersistent {
  165. get { return persistent; }
  166. }
  167. public DateTime IssueDate {
  168. get { return issue_date; }
  169. }
  170. public string Name {
  171. get { return name; }
  172. }
  173. public string UserData {
  174. get { return user_data; }
  175. }
  176. public int Version {
  177. get { return version; }
  178. }
  179. }
  180. }