svn path=/branches/mono-1-1-9/mcs/; revision=50438
[mono.git] / mcs / class / System.Web / System.Web.Security / FormsAuthenticationTicket.cs
1 //
2 // System.Web.Security.FormsAuthenticationTicket
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.IO;
34
35 namespace System.Web.Security
36 {
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                 /*
49                 internal void ToStr ()
50                 {
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);
58                 }
59                 */
60
61                 internal byte [] ToByteArray ()
62                 {
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);
70                         if (name != null)
71                                 writer.Write (name);
72
73                         writer.Write (cookie_path != null);
74                         if (cookie_path != null)
75                                 writer.Write (cookie_path);
76
77                         writer.Write (user_data != null);
78                         if (user_data != null)
79                                 writer.Write (user_data);
80
81                         writer.Flush ();
82                         return ms.ToArray ();
83                 }
84
85                 internal static FormsAuthenticationTicket FromByteArray (byte [] bytes)
86                 {
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 ();
96
97                         if (reader.ReadBoolean ())
98                                 ticket.cookie_path = reader.ReadString ();
99
100                         if (reader.ReadBoolean ())
101                                 ticket.user_data = reader.ReadString ();
102
103                         return ticket;
104                 }
105
106                 private FormsAuthenticationTicket ()
107                 {
108                 }
109
110                 public FormsAuthenticationTicket (int version,
111                                                   string name,
112                                                   DateTime issueDate,
113                                                   DateTime expiration,
114                                                   bool isPersistent,
115                                                   string userData)
116                 {
117                         this.version = version;
118                         this.name = name;
119                         this.issue_date = issueDate;
120                         this.expiration = expiration;
121                         this.persistent = isPersistent;
122                         this.user_data = userData;
123                         this.cookie_path = "/";
124                 }
125
126                 public FormsAuthenticationTicket (int version,
127                                                   string name,
128                                                   DateTime issueDate,
129                                                   DateTime expiration,
130                                                   bool isPersistent,
131                                                   string userData,
132                                                   string cookiePath)
133                 {
134                         this.version = version;
135                         this.name = name;
136                         this.issue_date = issueDate;
137                         this.expiration = expiration;
138                         this.persistent = isPersistent;
139                         this.user_data = userData;
140                         this.cookie_path = cookiePath;
141                 }
142
143                 public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
144                 {
145                         this.version = 1;
146                         this.name = name;
147                         this.issue_date = DateTime.Now;
148                         this.persistent = isPersistent;
149                         if (persistent)
150                                 expiration = issue_date.AddYears (50);
151                         else
152                                 expiration = issue_date.AddMinutes ((double) timeout);
153
154                         this.user_data = "";
155                         this.cookie_path = "/";
156                 }
157
158                 internal void SetDates (DateTime issue_date, DateTime expiration)
159                 {
160                         this.issue_date = issue_date;
161                         this.expiration = expiration;
162                 }
163                 
164                 internal FormsAuthenticationTicket Clone ()
165                 {
166                         return new FormsAuthenticationTicket   (version,
167                                                                 name,
168                                                                 issue_date,
169                                                                 expiration,
170                                                                 persistent,
171                                                                 user_data,
172                                                                 cookie_path);
173                 }
174
175                 public string CookiePath {
176                         get { return cookie_path; }
177                 }
178
179                 public DateTime Expiration {
180                         get { return expiration; }
181                 }
182
183                 public bool Expired {
184                         get { return DateTime.Now > expiration; }
185                 }
186
187                 public bool IsPersistent {
188                         get { return persistent; }
189                 }
190
191                 public DateTime IssueDate {
192                         get { return issue_date; }
193                 }
194
195                 public string Name {
196                         get { return name; }
197                 }
198
199                 public string UserData {
200                         get { return user_data; }
201                 }
202
203                 public int Version {
204                         get { return version; }
205                 }
206         }
207 }
208