2 // System.Web.Security.FormsAuthenticationTicket
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Security.Permissions;
35 namespace System.Web.Security
37 // CAS - no InheritanceDemand here as the class is sealed
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public sealed class FormsAuthenticationTicket
52 internal void ToStr ()
54 Console.WriteLine ("version: {0}", version);
55 Console.WriteLine ("persistent: {0}", persistent);
56 Console.WriteLine ("issue_date: {0}", issue_date);
57 Console.WriteLine ("expiration: {0}", expiration);
58 Console.WriteLine ("name: {0}", name);
59 Console.WriteLine ("cookie_path: {0}", cookie_path);
60 Console.WriteLine ("user_data: {0}", user_data);
64 internal byte [] ToByteArray ()
66 MemoryStream ms = new MemoryStream ();
67 BinaryWriter writer = new BinaryWriter (ms);
68 writer.Write (version);
69 writer.Write (persistent);
70 writer.Write (issue_date.Ticks);
71 writer.Write (expiration.Ticks);
72 writer.Write (name != null);
76 writer.Write (cookie_path != null);
77 if (cookie_path != null)
78 writer.Write (cookie_path);
80 writer.Write (user_data != null);
81 if (user_data != null)
82 writer.Write (user_data);
88 internal static FormsAuthenticationTicket FromByteArray (byte [] bytes)
91 throw new ArgumentNullException ("bytes");
93 MemoryStream ms = new MemoryStream (bytes);
94 BinaryReader reader = new BinaryReader (ms);
95 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket ();
96 ticket.version = reader.ReadInt32 ();
97 ticket.persistent = reader.ReadBoolean ();
98 ticket.issue_date = new DateTime (reader.ReadInt64 ());
99 ticket.expiration = new DateTime (reader.ReadInt64 ());
100 if (reader.ReadBoolean ())
101 ticket.name = reader.ReadString ();
103 if (reader.ReadBoolean ())
104 ticket.cookie_path = reader.ReadString ();
106 if (reader.ReadBoolean ())
107 ticket.user_data = reader.ReadString ();
112 FormsAuthenticationTicket ()
116 public FormsAuthenticationTicket (int version,
123 this.version = version;
125 this.issue_date = issueDate;
126 this.expiration = expiration;
127 this.persistent = isPersistent;
128 this.user_data = userData;
129 this.cookie_path = "/";
132 public FormsAuthenticationTicket (int version,
140 this.version = version;
142 this.issue_date = issueDate;
143 this.expiration = expiration;
144 this.persistent = isPersistent;
145 this.user_data = userData;
146 this.cookie_path = cookiePath;
149 public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
153 this.issue_date = DateTime.Now;
154 this.persistent = isPersistent;
156 expiration = issue_date.AddYears (50);
158 expiration = issue_date.AddMinutes ((double) timeout);
161 this.cookie_path = "/";
164 internal void SetDates (DateTime issue_date, DateTime expiration)
166 this.issue_date = issue_date;
167 this.expiration = expiration;
170 internal FormsAuthenticationTicket Clone ()
172 return new FormsAuthenticationTicket (version,
181 public string CookiePath {
182 get { return cookie_path; }
185 public DateTime Expiration {
186 get { return expiration; }
189 public bool Expired {
190 get { return DateTime.Now > expiration; }
193 public bool IsPersistent {
194 get { return persistent; }
197 public DateTime IssueDate {
198 get { return issue_date; }
205 public string UserData {
206 get { return user_data; }
210 get { return version; }