merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[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.IO;
33 using System.Security.Permissions;
34
35 namespace System.Web.Security
36 {
37         // CAS - no InheritanceDemand here as the class is sealed
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         // attributes
40         [Serializable]
41         public sealed class FormsAuthenticationTicket
42         {
43                 int version;
44                 bool persistent;
45                 DateTime issue_date;
46                 DateTime expiration;
47                 string name;
48                 string cookie_path;
49                 string user_data;
50
51                 /*
52                 internal void ToStr ()
53                 {
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);
61                 }
62                 */
63
64                 internal byte [] ToByteArray ()
65                 {
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);
73                         if (name != null)
74                                 writer.Write (name);
75
76                         writer.Write (cookie_path != null);
77                         if (cookie_path != null)
78                                 writer.Write (cookie_path);
79
80                         writer.Write (user_data != null);
81                         if (user_data != null)
82                                 writer.Write (user_data);
83
84                         writer.Flush ();
85                         return ms.ToArray ();
86                 }
87
88                 internal static FormsAuthenticationTicket FromByteArray (byte [] bytes)
89                 {
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 ();
99
100                         if (reader.ReadBoolean ())
101                                 ticket.cookie_path = reader.ReadString ();
102
103                         if (reader.ReadBoolean ())
104                                 ticket.user_data = reader.ReadString ();
105
106                         return ticket;
107                 }
108
109                 private FormsAuthenticationTicket ()
110                 {
111                 }
112
113                 public FormsAuthenticationTicket (int version,
114                                                   string name,
115                                                   DateTime issueDate,
116                                                   DateTime expiration,
117                                                   bool isPersistent,
118                                                   string userData)
119                 {
120                         this.version = version;
121                         this.name = name;
122                         this.issue_date = issueDate;
123                         this.expiration = expiration;
124                         this.persistent = isPersistent;
125                         this.user_data = userData;
126                         this.cookie_path = "/";
127                 }
128
129                 public FormsAuthenticationTicket (int version,
130                                                   string name,
131                                                   DateTime issueDate,
132                                                   DateTime expiration,
133                                                   bool isPersistent,
134                                                   string userData,
135                                                   string cookiePath)
136                 {
137                         this.version = version;
138                         this.name = name;
139                         this.issue_date = issueDate;
140                         this.expiration = expiration;
141                         this.persistent = isPersistent;
142                         this.user_data = userData;
143                         this.cookie_path = cookiePath;
144                 }
145
146                 public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
147                 {
148                         this.version = 1;
149                         this.name = name;
150                         this.issue_date = DateTime.Now;
151                         this.persistent = isPersistent;
152                         if (persistent)
153                                 expiration = issue_date.AddYears (50);
154                         else
155                                 expiration = issue_date.AddMinutes ((double) timeout);
156
157                         this.user_data = "";
158                         this.cookie_path = "/";
159                 }
160
161                 internal void SetDates (DateTime issue_date, DateTime expiration)
162                 {
163                         this.issue_date = issue_date;
164                         this.expiration = expiration;
165                 }
166                 
167                 internal FormsAuthenticationTicket Clone ()
168                 {
169                         return new FormsAuthenticationTicket   (version,
170                                                                 name,
171                                                                 issue_date,
172                                                                 expiration,
173                                                                 persistent,
174                                                                 user_data,
175                                                                 cookie_path);
176                 }
177
178                 public string CookiePath {
179                         get { return cookie_path; }
180                 }
181
182                 public DateTime Expiration {
183                         get { return expiration; }
184                 }
185
186                 public bool Expired {
187                         get { return DateTime.Now > expiration; }
188                 }
189
190                 public bool IsPersistent {
191                         get { return persistent; }
192                 }
193
194                 public DateTime IssueDate {
195                         get { return issue_date; }
196                 }
197
198                 public string Name {
199                         get { return name; }
200                 }
201
202                 public string UserData {
203                         get { return user_data; }
204                 }
205
206                 public int Version {
207                         get { return version; }
208                 }
209         }
210 }
211