2007-05-02 Marek Habersack <mhabersack@novell.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                 static FormsAuthenticationTicket Decrypt2 (byte [] bytes)
239                 {
240                         if (protection == FormsProtectionEnum.None)
241                                 return FormsAuthenticationTicket.FromByteArray (bytes);
242
243 #if NET_2_0
244                         MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetSection (machineKeyConfigPath);
245 #else
246                         MachineKeyConfig config = HttpContext.GetAppConfig (machineKeyConfigPath) as MachineKeyConfig;
247 #endif
248                         bool all = (protection == FormsProtectionEnum.All);
249
250                         byte [] result = bytes;
251                         if (all || protection == FormsProtectionEnum.Encryption) {
252                                 ICryptoTransform decryptor;
253                                 decryptor = TripleDES.Create ().CreateDecryptor (config.DecryptionKey192Bits, init_vector);
254                                 result = decryptor.TransformFinalBlock (bytes, 0, bytes.Length);
255                                 bytes = null;
256                         }
257
258                         if (all || protection == FormsProtectionEnum.Validation) {
259                                 int count;
260                                 MachineKeyValidation validationType;
261
262 #if NET_2_0
263                                 validationType = config.Validation;
264 #else
265                                 validationType = config.ValidationType;
266 #endif
267                                 if (validationType == MachineKeyValidation.MD5)
268                                         count = MD5_hash_size;
269                                 else
270                                         count = SHA1_hash_size; // 3DES and SHA1
271
272 #if NET_2_0
273                                 byte [] vk = config.ValidationKeyBytes;
274 #else
275                                 byte [] vk = config.ValidationKey;
276 #endif
277                                 byte [] mix = new byte [result.Length - count + vk.Length];
278                                 Buffer.BlockCopy (result, 0, mix, 0, result.Length - count);
279                                 Buffer.BlockCopy (vk, 0, mix, result.Length - count, vk.Length);
280
281                                 byte [] hash = null;
282                                 switch (validationType) {
283                                 case MachineKeyValidation.MD5:
284                                         hash = MD5.Create ().ComputeHash (mix);
285                                         break;
286                                 // From MS docs: "When 3DES is specified, forms authentication defaults to SHA1"
287                                 case MachineKeyValidation.TripleDES:
288                                 case MachineKeyValidation.SHA1:
289                                         hash = SHA1.Create ().ComputeHash (mix);
290                                         break;
291                                 }
292
293                                 if (result.Length < count)
294                                         throw new ArgumentException ("Error validating ticket (length).", "encryptedTicket");
295
296                                 int i, k;
297                                 for (i = result.Length - count, k = 0; k < count; i++, k++) {
298                                         if (result [i] != hash [k])
299                                                 throw new ArgumentException ("Error validating ticket.", "encryptedTicket");
300                                 }
301                         }
302
303                         return FormsAuthenticationTicket.FromByteArray (result);
304                 }
305
306                 public static FormsAuthenticationTicket Decrypt (string encryptedTicket)
307                 {
308                         if (encryptedTicket == null || encryptedTicket == String.Empty)
309                                 throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
310
311                         Initialize ();
312
313                         FormsAuthenticationTicket ticket;
314 #if NET_2_0
315                         byte [] bytes = MachineKeySection.GetBytes (encryptedTicket, encryptedTicket.Length);
316 #else
317                         byte [] bytes = MachineKeyConfig.GetBytes (encryptedTicket, encryptedTicket.Length);
318 #endif
319                         try {
320                                 ticket = Decrypt2 (bytes);
321                         } catch (Exception) {
322                                 ticket = null;
323                         }
324
325                         return ticket;
326                 }
327
328                 public static string Encrypt (FormsAuthenticationTicket ticket)
329                 {
330                         if (ticket == null)
331                                 throw new ArgumentNullException ("ticket");
332
333                         Initialize ();
334                         byte [] ticket_bytes = ticket.ToByteArray ();
335                         if (protection == FormsProtectionEnum.None)
336                                 return GetHexString (ticket_bytes);
337
338                         byte [] result = ticket_bytes;
339 #if NET_2_0
340                         MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetSection (machineKeyConfigPath);
341 #else
342                         MachineKeyConfig config = HttpContext.GetAppConfig (machineKeyConfigPath) as MachineKeyConfig;
343 #endif
344                         bool all = (protection == FormsProtectionEnum.All);
345                         if (all || protection == FormsProtectionEnum.Validation) {
346                                 byte [] valid_bytes = null;
347 #if NET_2_0
348                                 byte [] vk = config.ValidationKeyBytes;
349 #else
350                                 byte [] vk = config.ValidationKey;
351 #endif
352                                 byte [] mix = new byte [ticket_bytes.Length + vk.Length];
353                                 Buffer.BlockCopy (ticket_bytes, 0, mix, 0, ticket_bytes.Length);
354                                 Buffer.BlockCopy (vk, 0, mix, result.Length, vk.Length);
355
356                                 switch (
357 #if NET_2_0
358                                         config.Validation
359 #else
360                                         config.ValidationType
361 #endif
362                                         ) {
363                                 case MachineKeyValidation.MD5:
364                                         valid_bytes = MD5.Create ().ComputeHash (mix);
365                                         break;
366                                 // From MS docs: "When 3DES is specified, forms authentication defaults to SHA1"
367                                 case MachineKeyValidation.TripleDES:
368                                 case MachineKeyValidation.SHA1:
369                                         valid_bytes = SHA1.Create ().ComputeHash (mix);
370                                         break;
371                                 }
372
373                                 int tlen = ticket_bytes.Length;
374                                 int vlen = valid_bytes.Length;
375                                 result = new byte [tlen + vlen];
376                                 Buffer.BlockCopy (ticket_bytes, 0, result, 0, tlen);
377                                 Buffer.BlockCopy (valid_bytes, 0, result, tlen, vlen);
378                         }
379
380                         if (all || protection == FormsProtectionEnum.Encryption) {
381                                 ICryptoTransform encryptor;
382                                 encryptor = TripleDES.Create ().CreateEncryptor (config.DecryptionKey192Bits, init_vector);
383                                 result = encryptor.TransformFinalBlock (result, 0, result.Length);
384                         }
385
386                         return GetHexString (result);
387                 }
388
389                 public static HttpCookie GetAuthCookie (string userName, bool createPersistentCookie)
390                 {
391                         return GetAuthCookie (userName, createPersistentCookie, null);
392                 }
393
394                 public static HttpCookie GetAuthCookie (string userName, bool createPersistentCookie, string strCookiePath)
395                 {
396                         Initialize ();
397
398                         if (userName == null)
399                                 userName = String.Empty;
400
401                         if (strCookiePath == null || strCookiePath.Length == 0)
402                                 strCookiePath = cookiePath;
403
404                         DateTime now = DateTime.Now;
405                         DateTime then;
406                         if (createPersistentCookie)
407                                 then = now.AddYears (50);
408                         else
409                                 then = now.AddMinutes (timeout);
410
411                         FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1,
412                                                                                           userName,
413                                                                                           now,
414                                                                                           then,
415                                                                                           createPersistentCookie,
416                                                                                           String.Empty,
417                                                                                           cookiePath);
418
419                         if (!createPersistentCookie)
420                                 then = DateTime.MinValue;
421
422                         HttpCookie cookie = new HttpCookie (cookieName, Encrypt (ticket), strCookiePath, then);
423                         if (requireSSL)
424                                 cookie.Secure = true;
425                         return cookie;
426                 }
427
428                 internal static string ReturnUrl
429                 {
430                         get { return HttpContext.Current.Request ["RETURNURL"]; }
431                 }
432
433                 public static string GetRedirectUrl (string userName, bool createPersistentCookie)
434                 {
435                         if (userName == null)
436                                 return null;
437
438                         Initialize ();
439                         HttpRequest request = HttpContext.Current.Request;
440                         string returnUrl = ReturnUrl;
441                         if (returnUrl != null)
442                                 return returnUrl;
443
444                         returnUrl = request.ApplicationPath;
445                         string apppath = request.PhysicalApplicationPath;
446                         bool found = false;
447
448                         foreach (string indexFile in indexFiles) {
449                                 string filePath = Path.Combine (apppath, indexFile);
450                                 if (File.Exists (filePath)) {
451                                         returnUrl = UrlUtils.Combine (returnUrl, indexFile);
452                                         found = true;
453                                         break;
454                                 }
455                         }
456
457                         if (!found)
458                                 returnUrl = UrlUtils.Combine (returnUrl, "index.aspx");
459
460                         return returnUrl;
461                 }
462
463                 static string GetHexString (byte [] bytes)
464                 {
465                         StringBuilder result = new StringBuilder (bytes.Length * 2);
466                         foreach (byte b in bytes)
467                                 result.AppendFormat ("{0:X2}", (int) b);
468
469                         return result.ToString ();
470                 }
471
472                 public static string HashPasswordForStoringInConfigFile (string password, string passwordFormat)
473                 {
474                         if (password == null)
475                                 throw new ArgumentNullException ("password");
476
477                         if (passwordFormat == null)
478                                 throw new ArgumentNullException ("passwordFormat");
479
480                         byte [] bytes;
481                         if (String.Compare (passwordFormat, "MD5", true, CultureInfo.InvariantCulture) == 0) {
482                                 bytes = MD5.Create ().ComputeHash (Encoding.UTF8.GetBytes (password));
483                         } else if (String.Compare (passwordFormat, "SHA1", true, CultureInfo.InvariantCulture) == 0) {
484                                 bytes = SHA1.Create ().ComputeHash (Encoding.UTF8.GetBytes (password));
485                         } else {
486                                 throw new ArgumentException ("The format must be either MD5 or SHA1", "passwordFormat");
487                         }
488
489                         return GetHexString (bytes);
490                 }
491
492                 public static void Initialize ()
493                 {
494                         if (initialized)
495                                 return;
496
497                         lock (locker) {
498                                 if (initialized)
499                                         return;
500
501 #if NET_2_0
502                                 AuthenticationSection section = (AuthenticationSection)WebConfigurationManager.GetSection (authConfigPath);
503                                 FormsAuthenticationConfiguration config = section.Forms;
504
505                                 cookieName = config.Name;
506                                 timeout = (int)config.Timeout.TotalMinutes;
507                                 cookiePath = config.Path;
508                                 protection = config.Protection;
509                                 requireSSL = config.RequireSSL;
510                                 slidingExpiration = config.SlidingExpiration;
511                                 cookie_domain = config.Domain;
512                                 cookie_mode = config.Cookieless;
513                                 cookies_supported = true; /* XXX ? */
514                                 default_url = MapUrl(config.DefaultUrl);
515                                 enable_crossapp_redirects = config.EnableCrossAppRedirects;
516                                 login_url = MapUrl(config.LoginUrl);
517 #else
518                                 HttpContext context = HttpContext.Current;
519                                 AuthConfig authConfig = context.GetConfig (authConfigPath) as AuthConfig;
520                                 if (authConfig != null) {
521                                         cookieName = authConfig.CookieName;
522                                         timeout = authConfig.Timeout;
523                                         cookiePath = authConfig.CookiePath;
524                                         protection = authConfig.Protection;
525 #if NET_1_1
526                                         requireSSL = authConfig.RequireSSL;
527                                         slidingExpiration = authConfig.SlidingExpiration;
528 #endif
529                                 } else {
530                                         cookieName = ".MONOAUTH";
531                                         timeout = 30;
532                                         cookiePath = "/";
533                                         protection = FormsProtectionEnum.All;
534 #if NET_1_1
535                                         slidingExpiration = true;
536 #endif
537                                 }
538 #endif
539
540                                 // IV is 8 bytes long for 3DES
541                                 init_vector = new byte [8];
542                                 int len = cookieName.Length;
543                                 for (int i = 0; i < 8; i++) {
544                                         if (i >= len)
545                                                 break;
546
547                                         init_vector [i] = (byte) cookieName [i];
548                                 }
549
550                                 initialized = true;
551                         }
552                 }
553
554 #if NET_2_0
555                 static string MapUrl (string url) {
556                         if (UrlUtils.IsRelativeUrl (url))
557                                 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
558                         else
559                                 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
560                 }
561 #endif
562
563                 public static void RedirectFromLoginPage (string userName, bool createPersistentCookie)
564                 {
565                         RedirectFromLoginPage (userName, createPersistentCookie, null);
566                 }
567
568                 public static void RedirectFromLoginPage (string userName, bool createPersistentCookie, string strCookiePath)
569                 {
570                         if (userName == null)
571                                 return;
572
573                         Initialize ();
574                         SetAuthCookie (userName, createPersistentCookie, strCookiePath);
575                         Redirect (GetRedirectUrl (userName, createPersistentCookie), false);
576                 }
577
578                 public static FormsAuthenticationTicket RenewTicketIfOld (FormsAuthenticationTicket tOld)
579                 {
580                         if (tOld == null)
581                                 return null;
582
583                         DateTime now = DateTime.Now;
584                         TimeSpan toIssue = now - tOld.IssueDate;
585                         TimeSpan toExpiration = tOld.Expiration - now;
586                         if (toExpiration > toIssue)
587                                 return tOld;
588
589                         FormsAuthenticationTicket tNew = tOld.Clone ();
590                         tNew.SetDates (now, now + (tOld.Expiration - tOld.IssueDate));
591                         return tNew;
592                 }
593
594                 public static void SetAuthCookie (string userName, bool createPersistentCookie)
595                 {
596                         Initialize ();
597                         SetAuthCookie (userName, createPersistentCookie, cookiePath);
598                 }
599
600                 public static void SetAuthCookie (string userName, bool createPersistentCookie, string strCookiePath)
601                 {
602                         HttpContext context = HttpContext.Current;
603                         if (context == null)
604                                 throw new HttpException ("Context is null!");
605
606                         HttpResponse response = context.Response;
607                         if (response == null)
608                                 throw new HttpException ("Response is null!");
609
610                         response.Cookies.Add (GetAuthCookie (userName, createPersistentCookie, strCookiePath));
611                 }
612
613                 public static void SignOut ()
614                 {
615                         Initialize ();
616
617                         HttpContext context = HttpContext.Current;
618                         if (context == null)
619                                 throw new HttpException ("Context is null!");
620
621                         HttpResponse response = context.Response;
622                         if (response == null)
623                                 throw new HttpException ("Response is null!");
624
625                         HttpCookieCollection cc = response.Cookies;
626                         cc.Remove (cookieName);
627                         HttpCookie expiration_cookie = new HttpCookie (cookieName, "");
628                         expiration_cookie.Expires = new DateTime (1999, 10, 12);
629                         expiration_cookie.Path = cookiePath;
630                         cc.Add (expiration_cookie);
631
632 #if NET_2_0
633                         Roles.DeleteCookie ();
634 #endif
635                 }
636
637                 public static string FormsCookieName
638                 {
639                         get {
640                                 Initialize ();
641                                 return cookieName;
642                         }
643                 }
644
645                 public static string FormsCookiePath
646                 {
647                         get {
648                                 Initialize ();
649                                 return cookiePath;
650                         }
651                 }
652 #if NET_1_1
653                 public static bool RequireSSL {
654                         get {
655                                 Initialize ();
656                                 return requireSSL;
657                         }
658                 }
659
660                 public static bool SlidingExpiration {
661                         get {
662                                 Initialize ();
663                                 return slidingExpiration;
664                         }
665                 }
666 #endif
667
668 #if NET_2_0
669                 public static string CookieDomain {
670                         get { Initialize (); return cookie_domain; }
671                 }
672
673                 public static HttpCookieMode CookieMode {
674                         get { Initialize (); return cookie_mode; }
675                 }
676
677                 public static bool CookiesSupported {
678                         get { Initialize (); return cookies_supported; }
679                 }
680
681                 public static string DefaultUrl {
682                         get { Initialize (); return default_url; }
683                 }
684
685                 public static bool EnableCrossAppRedirects {
686                         get { Initialize (); return enable_crossapp_redirects; }
687                 }
688
689                 public static string LoginUrl {
690                         get { Initialize (); return login_url; }
691                 }
692
693                 public static void RedirectToLoginPage ()
694                 {
695                         Redirect (LoginUrl);
696                 }
697
698                 [MonoTODO ("needs more tests")]
699                 public static void RedirectToLoginPage (string extraQueryString)
700                 {
701                         // TODO: if ? is in LoginUrl (legal?), ? in query (legal?) ...
702                         Redirect (LoginUrl + "?" + extraQueryString);
703                 }
704
705                 private static void Redirect (string url)
706                 {
707                         HttpContext.Current.Response.Redirect (url);
708                 }
709 #endif
710
711                 private static void Redirect (string url, bool end)
712                 {
713                         HttpContext.Current.Response.Redirect (url, end);
714                 }
715         }
716 }