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)
90 MemoryStream ms = new MemoryStream (bytes);
91 BinaryReader reader = new BinaryReader (ms);
92 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket ();
93 ticket.version = reader.ReadInt32 ();
94 ticket.persistent = reader.ReadBoolean ();
95 ticket.issue_date = new DateTime (reader.ReadInt64 ());
96 ticket.expiration = new DateTime (reader.ReadInt64 ());
97 if (reader.ReadBoolean ())
98 ticket.name = reader.ReadString ();
100 if (reader.ReadBoolean ())
101 ticket.cookie_path = reader.ReadString ();
103 if (reader.ReadBoolean ())
104 ticket.user_data = reader.ReadString ();
109 FormsAuthenticationTicket ()
113 public FormsAuthenticationTicket (int version,
120 this.version = version;
122 this.issue_date = issueDate;
123 this.expiration = expiration;
124 this.persistent = isPersistent;
125 this.user_data = userData;
126 this.cookie_path = "/";
129 public FormsAuthenticationTicket (int version,
137 this.version = version;
139 this.issue_date = issueDate;
140 this.expiration = expiration;
141 this.persistent = isPersistent;
142 this.user_data = userData;
143 this.cookie_path = cookiePath;
146 public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
150 this.issue_date = DateTime.Now;
151 this.persistent = isPersistent;
153 expiration = issue_date.AddYears (50);
155 expiration = issue_date.AddMinutes ((double) timeout);
158 this.cookie_path = "/";
161 internal void SetDates (DateTime issue_date, DateTime expiration)
163 this.issue_date = issue_date;
164 this.expiration = expiration;
167 internal FormsAuthenticationTicket Clone ()
169 return new FormsAuthenticationTicket (version,
178 public string CookiePath {
179 get { return cookie_path; }
182 public DateTime Expiration {
183 get { return expiration; }
186 public bool Expired {
187 get { return DateTime.Now > expiration; }
190 public bool IsPersistent {
191 get { return persistent; }
194 public DateTime IssueDate {
195 get { return issue_date; }
202 public string UserData {
203 get { return user_data; }
207 get { return version; }