FormsAuthenticationTicket.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.IO;
  31. using System.Security.Permissions;
  32. namespace System.Web.Security
  33. {
  34. // CAS - no InheritanceDemand here as the class is sealed
  35. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [Serializable]
  38. public sealed class FormsAuthenticationTicket
  39. {
  40. int version;
  41. bool persistent;
  42. DateTime issue_date;
  43. DateTime expiration;
  44. string name;
  45. string cookie_path;
  46. string user_data;
  47. /*
  48. internal void ToStr ()
  49. {
  50. Console.WriteLine ("version: {0}", version);
  51. Console.WriteLine ("persistent: {0}", persistent);
  52. Console.WriteLine ("issue_date: {0}", issue_date);
  53. Console.WriteLine ("expiration: {0}", expiration);
  54. Console.WriteLine ("name: {0}", name);
  55. Console.WriteLine ("cookie_path: {0}", cookie_path);
  56. Console.WriteLine ("user_data: {0}", user_data);
  57. }
  58. */
  59. internal byte [] ToByteArray ()
  60. {
  61. MemoryStream ms = new MemoryStream ();
  62. BinaryWriter writer = new BinaryWriter (ms);
  63. writer.Write (version);
  64. writer.Write (persistent);
  65. writer.Write (issue_date.Ticks);
  66. writer.Write (expiration.Ticks);
  67. writer.Write (name != null);
  68. if (name != null)
  69. writer.Write (name);
  70. writer.Write (cookie_path != null);
  71. if (cookie_path != null)
  72. writer.Write (cookie_path);
  73. writer.Write (user_data != null);
  74. if (user_data != null)
  75. writer.Write (user_data);
  76. writer.Flush ();
  77. return ms.ToArray ();
  78. }
  79. internal static FormsAuthenticationTicket FromByteArray (byte [] bytes)
  80. {
  81. MemoryStream ms = new MemoryStream (bytes);
  82. BinaryReader reader = new BinaryReader (ms);
  83. FormsAuthenticationTicket ticket = new FormsAuthenticationTicket ();
  84. ticket.version = reader.ReadInt32 ();
  85. ticket.persistent = reader.ReadBoolean ();
  86. ticket.issue_date = new DateTime (reader.ReadInt64 ());
  87. ticket.expiration = new DateTime (reader.ReadInt64 ());
  88. if (reader.ReadBoolean ())
  89. ticket.name = reader.ReadString ();
  90. if (reader.ReadBoolean ())
  91. ticket.cookie_path = reader.ReadString ();
  92. if (reader.ReadBoolean ())
  93. ticket.user_data = reader.ReadString ();
  94. return ticket;
  95. }
  96. private FormsAuthenticationTicket ()
  97. {
  98. }
  99. public FormsAuthenticationTicket (int version,
  100. string name,
  101. DateTime issueDate,
  102. DateTime expiration,
  103. bool isPersistent,
  104. string userData)
  105. {
  106. this.version = version;
  107. this.name = name;
  108. this.issue_date = issueDate;
  109. this.expiration = expiration;
  110. this.persistent = isPersistent;
  111. this.user_data = userData;
  112. this.cookie_path = "/";
  113. }
  114. public FormsAuthenticationTicket (int version,
  115. string name,
  116. DateTime issueDate,
  117. DateTime expiration,
  118. bool isPersistent,
  119. string userData,
  120. string cookiePath)
  121. {
  122. this.version = version;
  123. this.name = name;
  124. this.issue_date = issueDate;
  125. this.expiration = expiration;
  126. this.persistent = isPersistent;
  127. this.user_data = userData;
  128. this.cookie_path = cookiePath;
  129. }
  130. public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
  131. {
  132. this.version = 1;
  133. this.name = name;
  134. this.issue_date = DateTime.Now;
  135. this.persistent = isPersistent;
  136. if (persistent)
  137. expiration = issue_date.AddYears (50);
  138. else
  139. expiration = issue_date.AddMinutes ((double) timeout);
  140. this.user_data = "";
  141. this.cookie_path = "/";
  142. }
  143. internal void SetDates (DateTime issue_date, DateTime expiration)
  144. {
  145. this.issue_date = issue_date;
  146. this.expiration = expiration;
  147. }
  148. internal FormsAuthenticationTicket Clone ()
  149. {
  150. return new FormsAuthenticationTicket (version,
  151. name,
  152. issue_date,
  153. expiration,
  154. persistent,
  155. user_data,
  156. cookie_path);
  157. }
  158. public string CookiePath {
  159. get { return cookie_path; }
  160. }
  161. public DateTime Expiration {
  162. get { return expiration; }
  163. }
  164. public bool Expired {
  165. get { return DateTime.Now > expiration; }
  166. }
  167. public bool IsPersistent {
  168. get { return persistent; }
  169. }
  170. public DateTime IssueDate {
  171. get { return issue_date; }
  172. }
  173. public string Name {
  174. get { return name; }
  175. }
  176. public string UserData {
  177. get { return user_data; }
  178. }
  179. public int Version {
  180. get { return version; }
  181. }
  182. }
  183. }