Merge pull request #1275 from ranma42/fix-lib64
[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                         if (bytes == null)
91                                 throw new ArgumentNullException ("bytes");
92                         
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 ();
102
103                         if (reader.ReadBoolean ())
104                                 ticket.cookie_path = reader.ReadString ();
105
106                         if (reader.ReadBoolean ())
107                                 ticket.user_data = reader.ReadString ();
108
109                         return ticket;
110                 }
111
112                 FormsAuthenticationTicket ()
113                 {
114                 }
115
116                 public FormsAuthenticationTicket (int version,
117                                                   string name,
118                                                   DateTime issueDate,
119                                                   DateTime expiration,
120                                                   bool isPersistent,
121                                                   string userData)
122                 {
123                         this.version = version;
124                         this.name = name;
125                         this.issue_date = issueDate;
126                         this.expiration = expiration;
127                         this.persistent = isPersistent;
128                         this.user_data = userData;
129                         this.cookie_path = "/";
130                 }
131
132                 public FormsAuthenticationTicket (int version,
133                                                   string name,
134                                                   DateTime issueDate,
135                                                   DateTime expiration,
136                                                   bool isPersistent,
137                                                   string userData,
138                                                   string cookiePath)
139                 {
140                         this.version = version;
141                         this.name = name;
142                         this.issue_date = issueDate;
143                         this.expiration = expiration;
144                         this.persistent = isPersistent;
145                         this.user_data = userData;
146                         this.cookie_path = cookiePath;
147                 }
148
149                 public FormsAuthenticationTicket (string name, bool isPersistent, int timeout)
150                 {
151                         this.version = 1;
152                         this.name = name;
153                         this.issue_date = DateTime.Now;
154                         this.persistent = isPersistent;
155                         if (persistent)
156                                 expiration = issue_date.AddYears (50);
157                         else
158                                 expiration = issue_date.AddMinutes ((double) timeout);
159
160                         this.user_data = "";
161                         this.cookie_path = "/";
162                 }
163
164                 internal void SetDates (DateTime issue_date, DateTime expiration)
165                 {
166                         this.issue_date = issue_date;
167                         this.expiration = expiration;
168                 }
169                 
170                 internal FormsAuthenticationTicket Clone ()
171                 {
172                         return new FormsAuthenticationTicket   (version,
173                                                                 name,
174                                                                 issue_date,
175                                                                 expiration,
176                                                                 persistent,
177                                                                 user_data,
178                                                                 cookie_path);
179                 }
180
181                 public string CookiePath {
182                         get { return cookie_path; }
183                 }
184
185                 public DateTime Expiration {
186                         get { return expiration; }
187                 }
188
189                 public bool Expired {
190                         get { return DateTime.Now > expiration; }
191                 }
192
193                 public bool IsPersistent {
194                         get { return persistent; }
195                 }
196
197                 public DateTime IssueDate {
198                         get { return issue_date; }
199                 }
200
201                 public string Name {
202                         get { return name; }
203                 }
204
205                 public string UserData {
206                         get { return user_data; }
207                 }
208
209                 public int Version {
210                         get { return version; }
211                 }
212         }
213 }
214