2008-12-08 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Security / FormsAuthentication.cs
1 //
2 // System.Web.Security.FormsAuthentication
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 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.Collections;
33 using System.IO;
34 using System.Security.Cryptography;
35 using System.Security.Permissions;
36 using System.Text;
37 using System.Web;
38 using System.Web.Configuration;
39 using System.Web.Util;
40 using System.Globalization;
41
42 namespace System.Web.Security
43 {
44         // CAS - no InheritanceDemand here as the class is sealed
45         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46         public sealed class FormsAuthentication
47         {
48                 const int MD5_hash_size = 16;
49                 const int SHA1_hash_size = 20;
50
51                 static string authConfigPath = "system.web/authentication";
52                 static string machineKeyConfigPath = "system.web/machineKey";
53 #if TARGET_J2EE
54                 const string Forms_initialized = "Forms.initialized";
55                 const string Forms_cookieName = "Forms.cookieName";
56                 const string Forms_cookiePath = "Forms.cookiePath";
57                 const string Forms_timeout = "Forms.timeout";
58                 const string Forms_protection = "Forms.protection";
59                 const string Forms_init_vector = "Forms.init_vector";
60                 static bool initialized
61                 {
62                         get {
63                                 object o = AppDomain.CurrentDomain.GetData (Forms_initialized);
64                                 return o != null ? (bool) o : false;
65                         }
66                         set { AppDomain.CurrentDomain.SetData (Forms_initialized, value); }
67                 }
68                 static string cookieName
69                 {
70                         get { return (string) AppDomain.CurrentDomain.GetData (Forms_cookieName); }
71                         set { AppDomain.CurrentDomain.SetData (Forms_cookieName, value); }
72                 }
73                 static string cookiePath
74                 {
75                         get { return (string) AppDomain.CurrentDomain.GetData (Forms_cookiePath); }
76                         set { AppDomain.CurrentDomain.SetData (Forms_cookiePath, value); }
77                 }
78                 static int timeout
79                 {
80                         get {
81                                 object o = AppDomain.CurrentDomain.GetData (Forms_timeout);
82                                 return o != null ? (int) o : 0;
83                         }
84                         set { AppDomain.CurrentDomain.SetData (Forms_timeout, value); }
85                 }
86                 static FormsProtectionEnum protection
87                 {
88                         get { return (FormsProtectionEnum) AppDomain.CurrentDomain.GetData (Forms_protection); }
89                         set { AppDomain.CurrentDomain.SetData (Forms_protection, value); }
90                 }
91                 static byte [] init_vector
92                 {
93                         get { return (byte []) AppDomain.CurrentDomain.GetData (Forms_init_vector); }
94                         set { AppDomain.CurrentDomain.SetData (Forms_init_vector, value); }
95                 }
96                 static object locker = new object ();
97 #else
98                 static bool initialized;
99                 static string cookieName;
100                 static string cookiePath;
101                 static int timeout;
102                 static FormsProtectionEnum protection;
103                 static object locker = new object ();
104                 static byte [] init_vector; // initialization vector used for 3DES
105 #endif
106 #if NET_1_1
107 #if TARGET_J2EE
108                 const string Forms_requireSSL = "Forms.requireSSL";
109                 const string Forms_slidingExpiration = "Forms.slidingExpiration";
110
111                 static bool requireSSL
112                 {
113                         get {
114                                 object o = AppDomain.CurrentDomain.GetData (Forms_requireSSL);
115                                 return o != null ? (bool) o : false;
116                         }
117                         set { AppDomain.CurrentDomain.SetData (Forms_requireSSL, value); }
118                 }
119                 static bool slidingExpiration
120                 {
121                         get {
122                                 object o = AppDomain.CurrentDomain.GetData (Forms_slidingExpiration);
123                                 return o != null ? (bool) o : false;
124                         }
125                         set { AppDomain.CurrentDomain.SetData (Forms_slidingExpiration, value); }
126                 }
127 #else
128                 static bool requireSSL;
129                 static bool slidingExpiration;
130 #endif
131 #endif
132 #if NET_2_0
133 #if TARGET_J2EE
134                 const string Forms_cookie_domain = "Forms.cookie_domain";
135                 const string Forms_cookie_mode = "Forms.cookie_mode";
136                 const string Forms_cookies_supported = "Forms.cookies_supported";
137                 const string Forms_default_url = "Forms.default_url";
138                 const string Forms_enable_crossapp_redirects = "Forms.enable_crossapp_redirects";
139                 const string Forms_login_url = "Forms.login_url";
140                 static string cookie_domain
141                 {
142                         get { return (string) AppDomain.CurrentDomain.GetData (Forms_cookie_domain); }
143                         set { AppDomain.CurrentDomain.SetData (Forms_cookie_domain, value); }
144                 }
145                 static HttpCookieMode cookie_mode
146                 {
147                         get { return (HttpCookieMode) AppDomain.CurrentDomain.GetData (Forms_cookie_mode); }
148                         set { AppDomain.CurrentDomain.SetData (Forms_cookie_mode, value); }
149                 }
150                 static bool cookies_supported
151                 {
152                         get {
153                                 object o = AppDomain.CurrentDomain.GetData (Forms_cookies_supported);
154                                 return o != null ? (bool) o : false;
155                         }
156                         set { AppDomain.CurrentDomain.SetData (Forms_cookies_supported, value); }
157                 }
158                 static string default_url
159                 {
160                         get { return (string) AppDomain.CurrentDomain.GetData (Forms_default_url); }
161                         set { AppDomain.CurrentDomain.SetData (Forms_default_url, value); }
162                 }
163                 static bool enable_crossapp_redirects
164                 {
165                         get {
166                                 object o = AppDomain.CurrentDomain.GetData (Forms_enable_crossapp_redirects);
167                                 return o != null ? (bool) o : false;
168                         }
169                         set { AppDomain.CurrentDomain.SetData (Forms_enable_crossapp_redirects, value); }
170                 }
171                 static string login_url
172                 {
173                         get { return (string) AppDomain.CurrentDomain.GetData (Forms_login_url); }
174                         set { AppDomain.CurrentDomain.SetData (Forms_login_url, value); }
175                 }
176 #else
177                 static string cookie_domain;
178                 static HttpCookieMode cookie_mode;
179                 static bool cookies_supported;
180                 static string default_url;
181                 static bool enable_crossapp_redirects;
182                 static string login_url;
183 #endif
184 #endif
185                 // same names and order used in xsp
186                 static string [] indexFiles = { "index.aspx",
187                                                 "Default.aspx",
188                                                 "default.aspx",
189                                                 "index.html",
190                                                 "index.htm" };
191
192                 public FormsAuthentication ()
193                 {
194                 }
195
196                 public static bool Authenticate (string name, string password)
197                 {
198                         if (name == null || password == null)
199                                 return false;
200
201                         Initialize ();
202                         HttpContext context = HttpContext.Current;
203                         if (context == null)
204                                 throw new HttpException ("Context is null!");
205
206                         name = name.ToLower ();
207 #if NET_2_0
208                         AuthenticationSection section = (AuthenticationSection) WebConfigurationManager.GetSection (authConfigPath);
209                         FormsAuthenticationCredentials config = section.Forms.Credentials;
210                         FormsAuthenticationUser user = config.Users[name];
211                         string stored = null;
212
213                         if (user != null)
214                                 stored = user.Password;
215 #else
216                         AuthConfig config = context.GetConfig (authConfigPath) as AuthConfig;
217                         Hashtable users = config.CredentialUsers;
218                         string stored = users [name] as string;
219 #endif
220                         if (stored == null)
221                                 return false;
222
223                         switch (config.PasswordFormat) {
224                         case FormsAuthPasswordFormat.Clear:
225                                 /* Do nothing */
226                                 break;
227                         case FormsAuthPasswordFormat.MD5:
228                                 password = HashPasswordForStoringInConfigFile (password, "MD5");
229                                 break;
230                         case FormsAuthPasswordFormat.SHA1:
231                                 password = HashPasswordForStoringInConfigFile (password, "SHA1");
232                                 break;
233                         }
234
235                         return (password == stored);
236                 }
237
238 #if NET_2_0
239                 static byte [] GetDecryptionKey (MachineKeySection config)
240                 {
241                         return MachineKeySectionUtils.DecryptionKey192Bits (config);
242                 }
243 #else
244                 static byte [] GetDecryptionKey (MachineKeyConfig config)
245                 {
246                         return config.DecryptionKey192Bits;
247                 }
248 #endif
249                 
250                 static FormsAuthenticationTicket Decrypt2 (byte [] bytes)
251                 {
252                         if (protection == FormsProtectionEnum.None)
253                                 return FormsAuthenticationTicket.FromByteArray (bytes);
254
255 #if NET_2_0
256                         MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetSection (machineKeyConfigPath);
257 #else
258                         MachineKeyConfig config = HttpContext.GetAppConfig (machineKeyConfigPath) as MachineKeyConfig;
259 #endif
260                         bool all = (protection == FormsProtectionEnum.All);
261
262                         byte [] result = bytes;
263                         if (all || protection == FormsProtectionEnum.Encryption) {
264                                 ICryptoTransform decryptor;
265                                 decryptor = TripleDES.Create ().CreateDecryptor (GetDecryptionKey (config), init_vector);
266                                 result = decryptor.TransformFinalBlock (bytes, 0, bytes.Length);
267                                 bytes = null;
268                         }
269
270                         if (all || protection == FormsProtectionEnum.Validation) {
271                                 int count;
272                                 MachineKeyValidation validationType;
273
274 #if NET_2_0
275                                 validationType = config.Validation;
276 #else
277                                 validationType = config.ValidationType;
278 #endif
279                                 if (validationType == MachineKeyValidation.MD5)
280                                         count = MD5_hash_size;
281                                 else
282                                         count = SHA1_hash_size; // 3DES and SHA1
283
284 #if NET_2_0
285                                 byte [] vk = MachineKeySectionUtils.ValidationKeyBytes (config);
286 #else
287                                 byte [] vk = config.ValidationKey;
288 #endif
289                                 byte [] mix = new byte [result.Length - count + vk.Length];
290                                 Buffer.BlockCopy (result, 0, mix, 0, result.Length - count);
291                                 Buffer.BlockCopy (vk, 0, mix, result.Length - count, vk.Length);
292
293                                 byte [] hash = null;
294                                 switch (validationType) {
295                                 case MachineKeyValidation.MD5:
296                                         hash = MD5.Create ().ComputeHash (mix);
297                                         break;
298                                 // From MS docs: "When 3DES is specified, forms authentication defaults to SHA1"
299                                 case MachineKeyValidation.TripleDES:
300                                 case MachineKeyValidation.SHA1:
301                                         hash = SHA1.Create ().ComputeHash (mix);
302                                         break;
303                                 }
304
305                                 if (result.Length < count)
306                                         throw new ArgumentException ("Error validating ticket (length).", "encryptedTicket");
307
308                                 int i, k;
309                                 for (i = result.Length - count, k = 0; k < count; i++, k++) {
310                                         if (result [i] != hash [k])
311                                                 throw new ArgumentException ("Error validating ticket.", "encryptedTicket");
312                                 }
313                         }
314
315                         return FormsAuthenticationTicket.FromByteArray (result);
316                 }
317
318                 public static FormsAuthenticationTicket Decrypt (string encryptedTicket)
319                 {
320                         if (encryptedTicket == null || encryptedTicket == String.Empty)
321                                 throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
322
323                         Initialize ();
324
325                         FormsAuthenticationTicket ticket;
326 #if NET_2_0
327                         byte [] bytes = MachineKeySectionUtils.GetBytes (encryptedTicket, encryptedTicket.Length);
328 #else
329                         byte [] bytes = MachineKeyConfig.GetBytes (encryptedTicket, encryptedTicket.Length);
330 #endif
331                         try {
332                                 ticket = Decrypt2 (bytes);
333                         } catch (Exception) {
334                                 ticket = null;
335                         }
336
337                         return ticket;
338                 }
339
340                 public static string Encrypt (FormsAuthenticationTicket ticket)
341                 {
342                         if (ticket == null)
343                                 throw new ArgumentNullException ("ticket");
344
345                         Initialize ();
346                         byte [] ticket_bytes = ticket.ToByteArray ();
347                         if (protection == FormsProtectionEnum.None)
348                                 return GetHexString (ticket_bytes);
349
350                         byte [] result = ticket_bytes;
351 #if NET_2_0
352                         MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetSection (machineKeyConfigPath);
353 #else
354                         MachineKeyConfig config = HttpContext.GetAppConfig (machineKeyConfigPath) as MachineKeyConfig;
355 #endif
356                         bool all = (protection == FormsProtectionEnum.All);
357                         if (all || protection == FormsProtectionEnum.Validation) {
358                                 byte [] valid_bytes = null;
359 #if NET_2_0
360                                 byte [] vk = MachineKeySectionUtils.ValidationKeyBytes (config);
361 #else
362                                 byte [] vk = config.ValidationKey;
363 #endif
364                                 byte [] mix = new byte [ticket_bytes.Length + vk.Length];
365                                 Buffer.BlockCopy (ticket_bytes, 0, mix, 0, ticket_bytes.Length);
366                                 Buffer.BlockCopy (vk, 0, mix, result.Length, vk.Length);
367
368                                 switch (
369 #if NET_2_0
370                                         config.Validation
371 #else
372                                         config.ValidationType
373 #endif
374                                         ) {
375                                 case MachineKeyValidation.MD5:
376                                         valid_bytes = MD5.Create ().ComputeHash (mix);
377                                         break;
378                                 // From MS docs: "When 3DES is specified, forms authentication defaults to SHA1"
379                                 case MachineKeyValidation.TripleDES:
380                                 case MachineKeyValidation.SHA1:
381                                         valid_bytes = SHA1.Create ().ComputeHash (mix);
382                                         break;
383                                 }
384
385                                 int tlen = ticket_bytes.Length;
386                                 int vlen = valid_bytes.Length;
387                                 result = new byte [tlen + vlen];
388                                 Buffer.BlockCopy (ticket_bytes, 0, result, 0, tlen);
389                                 Buffer.BlockCopy (valid_bytes, 0, result, tlen, vlen);
390                         }
391
392                         if (all || protection == FormsProtectionEnum.Encryption) {
393                                 ICryptoTransform encryptor;
394                                 encryptor = TripleDES.Create ().CreateEncryptor (GetDecryptionKey (config), init_vector);
395                                 result = encryptor.TransformFinalBlock (result, 0, result.Length);
396                         }
397
398                         return GetHexString (result);
399                 }
400
401                 public static HttpCookie GetAuthCookie (string userName, bool createPersistentCookie)
402                 {
403                         return GetAuthCookie (userName, createPersistentCookie, null);
404                 }
405
406                 public static HttpCookie GetAuthCookie (string userName, bool createPersistentCookie, string strCookiePath)
407                 {
408                         Initialize ();
409
410                         if (userName == null)
411                                 userName = String.Empty;
412
413                         if (strCookiePath == null || strCookiePath.Length == 0)
414                                 strCookiePath = cookiePath;
415
416                         DateTime now = DateTime.Now;
417                         DateTime then;
418                         if (createPersistentCookie)
419                                 then = now.AddYears (50);
420                         else
421                                 then = now.AddMinutes (timeout);
422
423                         FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1,
424                                                                                           userName,
425                                                                                           now,
426                                                                                           then,
427                                                                                           createPersistentCookie,
428                                                                                           String.Empty,
429                                                                                           cookiePath);
430
431                         if (!createPersistentCookie)
432                                 then = DateTime.MinValue;
433
434                         HttpCookie cookie = new HttpCookie (cookieName, Encrypt (ticket), strCookiePath, then);
435                         if (requireSSL)
436                                 cookie.Secure = true;
437                         return cookie;
438                 }
439
440                 internal static string ReturnUrl
441                 {
442                         get { return HttpContext.Current.Request ["RETURNURL"]; }
443                 }
444
445                 public static string GetRedirectUrl (string userName, bool createPersistentCookie)
446                 {
447                         if (userName == null)
448                                 return null;
449
450                         Initialize ();
451                         HttpRequest request = HttpContext.Current.Request;
452                         string returnUrl = ReturnUrl;
453                         if (returnUrl != null)
454                                 return returnUrl;
455
456                         returnUrl = request.ApplicationPath;
457                         string apppath = request.PhysicalApplicationPath;
458                         bool found = false;
459
460                         foreach (string indexFile in indexFiles) {
461                                 string filePath = Path.Combine (apppath, indexFile);
462                                 if (File.Exists (filePath)) {
463                                         returnUrl = UrlUtils.Combine (returnUrl, indexFile);
464                                         found = true;
465                                         break;
466                                 }
467                         }
468
469                         if (!found)
470                                 returnUrl = UrlUtils.Combine (returnUrl, "index.aspx");
471
472                         return returnUrl;
473                 }
474
475                 static string GetHexString (byte [] bytes)
476                 {
477                         const int letterPart = 55;
478                         const int numberPart = 48;
479                         char [] result = new char [bytes.Length * 2];
480                         for (int i = 0; i < bytes.Length; i++) {
481                                 int tmp = (int) bytes [i];
482                                 int second = tmp & 15;
483                                 int first = (tmp >> 4) & 15;
484                                 result [(i * 2)] = (char) (first > 9 ? letterPart + first : numberPart + first);
485                                 result [(i * 2) + 1] = (char) (second > 9 ? letterPart + second : numberPart + second);
486                         }
487                         return new string (result);
488                 }
489
490                 public static string HashPasswordForStoringInConfigFile (string password, string passwordFormat)
491                 {
492                         if (password == null)
493                                 throw new ArgumentNullException ("password");
494
495                         if (passwordFormat == null)
496                                 throw new ArgumentNullException ("passwordFormat");
497
498                         byte [] bytes;
499                         if (String.Compare (passwordFormat, "MD5", true, CultureInfo.InvariantCulture) == 0) {
500                                 bytes = MD5.Create ().ComputeHash (Encoding.UTF8.GetBytes (password));
501                         } else if (String.Compare (passwordFormat, "SHA1", true, CultureInfo.InvariantCulture) == 0) {
502                                 bytes = SHA1.Create ().ComputeHash (Encoding.UTF8.GetBytes (password));
503                         } else {
504                                 throw new ArgumentException ("The format must be either MD5 or SHA1", "passwordFormat");
505                         }
506
507                         return GetHexString (bytes);
508                 }
509
510                 public static void Initialize ()
511                 {
512                         if (initialized)
513                                 return;
514
515                         lock (locker) {
516                                 if (initialized)
517                                         return;
518
519 #if NET_2_0
520                                 AuthenticationSection section = (AuthenticationSection)WebConfigurationManager.GetSection (authConfigPath);
521                                 FormsAuthenticationConfiguration config = section.Forms;
522
523                                 cookieName = config.Name;
524                                 timeout = (int)config.Timeout.TotalMinutes;
525                                 cookiePath = config.Path;
526                                 protection = config.Protection;
527                                 requireSSL = config.RequireSSL;
528                                 slidingExpiration = config.SlidingExpiration;
529                                 cookie_domain = config.Domain;
530                                 cookie_mode = config.Cookieless;
531                                 cookies_supported = true; /* XXX ? */
532                                 default_url = MapUrl(config.DefaultUrl);
533                                 enable_crossapp_redirects = config.EnableCrossAppRedirects;
534                                 login_url = MapUrl(config.LoginUrl);
535 #else
536                                 HttpContext context = HttpContext.Current;
537                                 AuthConfig authConfig = context.GetConfig (authConfigPath) as AuthConfig;
538                                 if (authConfig != null) {
539                                         cookieName = authConfig.CookieName;
540                                         timeout = authConfig.Timeout;
541                                         cookiePath = authConfig.CookiePath;
542                                         protection = authConfig.Protection;
543 #if NET_1_1
544                                         requireSSL = authConfig.RequireSSL;
545                                         slidingExpiration = authConfig.SlidingExpiration;
546 #endif
547                                 } else {
548                                         cookieName = ".MONOAUTH";
549                                         timeout = 30;
550                                         cookiePath = "/";
551                                         protection = FormsProtectionEnum.All;
552 #if NET_1_1
553                                         slidingExpiration = true;
554 #endif
555                                 }
556 #endif
557
558                                 // IV is 8 bytes long for 3DES
559                                 init_vector = new byte [8];
560                                 int len = cookieName.Length;
561                                 for (int i = 0; i < 8; i++) {
562                                         if (i >= len)
563                                                 break;
564
565                                         init_vector [i] = (byte) cookieName [i];
566                                 }
567
568                                 initialized = true;
569                         }
570                 }
571
572 #if NET_2_0
573                 static string MapUrl (string url) {
574                         if (UrlUtils.IsRelativeUrl (url))
575                                 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
576                         else
577                                 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
578                 }
579 #endif
580
581                 public static void RedirectFromLoginPage (string userName, bool createPersistentCookie)
582                 {
583                         RedirectFromLoginPage (userName, createPersistentCookie, null);
584                 }
585
586                 public static void RedirectFromLoginPage (string userName, bool createPersistentCookie, string strCookiePath)
587                 {
588                         if (userName == null)
589                                 return;
590
591                         Initialize ();
592                         SetAuthCookie (userName, createPersistentCookie, strCookiePath);
593                         Redirect (GetRedirectUrl (userName, createPersistentCookie), false);
594                 }
595
596                 public static FormsAuthenticationTicket RenewTicketIfOld (FormsAuthenticationTicket tOld)
597                 {
598                         if (tOld == null)
599                                 return null;
600
601                         DateTime now = DateTime.Now;
602                         TimeSpan toIssue = now - tOld.IssueDate;
603                         TimeSpan toExpiration = tOld.Expiration - now;
604                         if (toExpiration > toIssue)
605                                 return tOld;
606
607                         FormsAuthenticationTicket tNew = tOld.Clone ();
608                         tNew.SetDates (now, now + (tOld.Expiration - tOld.IssueDate));
609                         return tNew;
610                 }
611
612                 public static void SetAuthCookie (string userName, bool createPersistentCookie)
613                 {
614                         Initialize ();
615                         SetAuthCookie (userName, createPersistentCookie, cookiePath);
616                 }
617
618                 public static void SetAuthCookie (string userName, bool createPersistentCookie, string strCookiePath)
619                 {
620                         HttpContext context = HttpContext.Current;
621                         if (context == null)
622                                 throw new HttpException ("Context is null!");
623
624                         HttpResponse response = context.Response;
625                         if (response == null)
626                                 throw new HttpException ("Response is null!");
627
628                         response.Cookies.Add (GetAuthCookie (userName, createPersistentCookie, strCookiePath));
629                 }
630
631                 public static void SignOut ()
632                 {
633                         Initialize ();
634
635                         HttpContext context = HttpContext.Current;
636                         if (context == null)
637                                 throw new HttpException ("Context is null!");
638
639                         HttpResponse response = context.Response;
640                         if (response == null)
641                                 throw new HttpException ("Response is null!");
642
643                         HttpCookieCollection cc = response.Cookies;
644                         cc.Remove (cookieName);
645                         HttpCookie expiration_cookie = new HttpCookie (cookieName, "");
646                         expiration_cookie.Expires = new DateTime (1999, 10, 12);
647                         expiration_cookie.Path = cookiePath;
648                         cc.Add (expiration_cookie);
649
650 #if NET_2_0
651                         Roles.DeleteCookie ();
652 #endif
653                 }
654
655                 public static string FormsCookieName
656                 {
657                         get {
658                                 Initialize ();
659                                 return cookieName;
660                         }
661                 }
662
663                 public static string FormsCookiePath
664                 {
665                         get {
666                                 Initialize ();
667                                 return cookiePath;
668                         }
669                 }
670 #if NET_1_1
671                 public static bool RequireSSL {
672                         get {
673                                 Initialize ();
674                                 return requireSSL;
675                         }
676                 }
677
678                 public static bool SlidingExpiration {
679                         get {
680                                 Initialize ();
681                                 return slidingExpiration;
682                         }
683                 }
684 #endif
685
686 #if NET_2_0
687                 public static string CookieDomain {
688                         get { Initialize (); return cookie_domain; }
689                 }
690
691                 public static HttpCookieMode CookieMode {
692                         get { Initialize (); return cookie_mode; }
693                 }
694
695                 public static bool CookiesSupported {
696                         get { Initialize (); return cookies_supported; }
697                 }
698
699                 public static string DefaultUrl {
700                         get { Initialize (); return default_url; }
701                 }
702
703                 public static bool EnableCrossAppRedirects {
704                         get { Initialize (); return enable_crossapp_redirects; }
705                 }
706
707                 public static string LoginUrl {
708                         get { Initialize (); return login_url; }
709                 }
710
711                 public static void RedirectToLoginPage ()
712                 {
713                         Redirect (LoginUrl);
714                 }
715
716                 [MonoTODO ("needs more tests")]
717                 public static void RedirectToLoginPage (string extraQueryString)
718                 {
719                         // TODO: if ? is in LoginUrl (legal?), ? in query (legal?) ...
720                         Redirect (LoginUrl + "?" + extraQueryString);
721                 }
722
723                 static void Redirect (string url)
724                 {
725                         HttpContext.Current.Response.Redirect (url);
726                 }
727 #endif
728
729                 static void Redirect (string url, bool end)
730                 {
731                         HttpContext.Current.Response.Redirect (url, end);
732                 }
733         }
734 }