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.
35 namespace System.Web.Security
38 public sealed class FormsAuthenticationTicket
49 internal void ToStr ()
51 Console.WriteLine ("version: {0}", version);
52 Console.WriteLine ("persistent: {0}", persistent);
53 Console.WriteLine ("issue_date: {0}", issue_date);
54 Console.WriteLine ("expiration: {0}", expiration);
55 Console.WriteLine ("name: {0}", name);
56 Console.WriteLine ("cookie_path: {0}", cookie_path);
57 Console.WriteLine ("user_data: {0}", user_data);
61 internal byte [] ToByteArray ()
63 MemoryStream ms = new MemoryStream ();
64 BinaryWriter writer = new BinaryWriter (ms);
65 writer.Write (version);
66 writer.Write (persistent);
67 writer.Write (issue_date.Ticks);
68 writer.Write (expiration.Ticks);
69 writer.Write (name != null);
73 writer.Write (cookie_path != null);
74 if (cookie_path != null)
75 writer.Write (cookie_path);
77 writer.Write (user_data != null);
78 if (user_data != null)
79 writer.Write (user_data);
85 internal static FormsAuthenticationTicket FromByteArray (byte [] bytes)
87 MemoryStream ms = new MemoryStream (bytes);
88 BinaryReader reader = new BinaryReader (ms);
89 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket ();
90 ticket.version = reader.ReadInt32 ();
91 ticket.persistent = reader.ReadBoolean ();
92 ticket.issue_date = new DateTime (reader.ReadInt64 ());
93 ticket.expiration = new DateTime (reader.ReadInt64 ());
94 if (reader.ReadBoolean ())
95 ticket.name = reader.ReadString ();
97 if (reader.ReadBoolean ())
98 ticket.cookie_path = reader.ReadString ();
100 if (reader.ReadBoolean ())
101 ticket.user_data = reader.ReadString ();
106 private FormsAuthenticationTicket ()
110 public FormsAuthenticationTicket (int version,
117 this.version = version;
119 this.issue_date = issueDate;
120 this.expiration = expiration;
121 this.persistent = isPersistent;
122 this.user_data = userData;
123 this.cookie_path = "/";
126 public FormsAuthenticationTicket (int version,
134 this.version = version;
136 this.issue_date = issueDate;
137 this.expiration = expiration;
138 this.persistent = isPersistent;
139 this.user_data = userData;
140 this.cookie_path = cookiePath;
143 public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
147 this.issue_date = DateTime.Now;
148 this.persistent = isPersistent;
150 expiration = issue_date.AddYears (50);
152 expiration = issue_date.AddMinutes ((double) timeout);
155 this.cookie_path = "/";
158 internal void SetDates (DateTime issue_date, DateTime expiration)
160 this.issue_date = issue_date;
161 this.expiration = expiration;
164 internal FormsAuthenticationTicket Clone ()
166 return new FormsAuthenticationTicket (version,
175 public string CookiePath {
176 get { return cookie_path; }
179 public DateTime Expiration {
180 get { return expiration; }
183 public bool Expired {
184 get { return DateTime.Now > expiration; }
187 public bool IsPersistent {
188 get { return persistent; }
191 public DateTime IssueDate {
192 get { return issue_date; }
199 public string UserData {
200 get { return user_data; }
204 get { return version; }