2008-11-06 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Security / AnonymousIdentificationModule.cs
index 9072b7e72d589876e2c06e26ef0a270b7e1eb7fb..b8e7bfa11a4c4284930441cb4f82ff7e5ba63055 100644 (file)
 
 #if NET_2_0
 
+using System;
+using System.ComponentModel;
 using System.Web;
 using System.Web.Configuration;
+using System.Text;
 
 namespace System.Web.Security {
 
        public sealed class AnonymousIdentificationModule : IHttpModule {
-
-               public event AnonymousIdentificationEventHandler Creating;
+               static readonly object creatingEvent = new object ();
+               
+               HttpApplication app;
+               EventHandlerList events = new EventHandlerList ();
                
+               public event AnonymousIdentificationEventHandler Creating  {
+                       add { events.AddHandler (creatingEvent, value); }
+                       remove { events.RemoveHandler (creatingEvent, value); }
+               }
+
                public static void ClearAnonymousIdentifier ()
                {
-                       HttpContext c = HttpContext.Current;
-                       SystemWebSectionGroup g = (SystemWebSectionGroup)WebConfigurationManager.GetSection ("system.web");
-
-                       if (!g.AnonymousIdentification.Enabled
-                           || false /* XXX The user for the current request is anonymous */)
+                       if (Config == null || !Config.Enabled)
+                               /* XXX The user for the current request is anonymous */
                                throw new NotSupportedException ();
                }
 
@@ -61,6 +68,7 @@ namespace System.Web.Security {
                        app.PostAuthenticateRequest += OnEnter;
                }
 
+               [MonoTODO ("cookieless userid")]
                void OnEnter (object source, EventArgs eventArgs)
                {
                        if (!Enabled)
@@ -68,27 +76,64 @@ namespace System.Web.Security {
 
                        string anonymousID = null;
 
-                       if (Creating != null) {
-                               AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs (HttpContext.Current);
-                               Creating (this, e);
-
-                               anonymousID = e.AnonymousID;
+                       HttpCookie cookie = app.Request.Cookies [Config.CookieName];
+                       if (cookie != null && (cookie.Expires == DateTime.MinValue || cookie.Expires > DateTime.Now)) {
+                               try {
+                                       anonymousID = Encoding.Unicode.GetString (Convert.FromBase64String (cookie.Value));
+                               }
+                               catch { }
                        }
 
-                       if (anonymousID == null)
-                               anonymousID = Guid.NewGuid().ToString();
+                       if (anonymousID == null) {
+                               AnonymousIdentificationEventHandler eh = events [creatingEvent] as AnonymousIdentificationEventHandler;
+                               if (eh != null) {
+                                       AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs (HttpContext.Current);
+                                       eh (this, e);
+
+                                       anonymousID = e.AnonymousID;
+                               }
 
+                               if (anonymousID == null)
+                                       anonymousID = Guid.NewGuid ().ToString ();
+
+                               HttpCookie newCookie = new HttpCookie (Config.CookieName);
+                               newCookie.Path = app.Request.ApplicationPath;
+                               newCookie.Expires = DateTime.Now + Config.CookieTimeout;
+                               newCookie.Value = Convert.ToBase64String (Encoding.Unicode.GetBytes (anonymousID));
+                               app.Response.AppendCookie (newCookie);
+                       }
                        app.Request.AnonymousID = anonymousID;
                }
 
                public static bool Enabled {
                        get {
-                               SystemWebSectionGroup g = (SystemWebSectionGroup)WebConfigurationManager.GetSection ("system.web");
-                               return g.AnonymousIdentification.Enabled;
+                               if (Config == null)
+                                       return false;
+
+                               return Config.Enabled;
                        }
                }
 
-               HttpApplication app;
+#if TARGET_JVM
+               static AnonymousIdentificationSection Config
+               {
+                       get
+                       {
+                               AnonymousIdentificationSection config = (AnonymousIdentificationSection) AppDomain.CurrentDomain.GetData ("Anonymous.Config");
+                               if (config == null) {
+                                       lock (typeof (AnonymousIdentificationModule)) {
+                                               config = (AnonymousIdentificationSection) AppDomain.CurrentDomain.GetData ("Anonymous.Config");
+                                               if (config == null)
+                                                       config = (AnonymousIdentificationSection) WebConfigurationManager.GetSection ("system.web/anonymousIdentification");
+                                               AppDomain.CurrentDomain.SetData ("Anonymous.Config", config);
+                                       }
+                               }
+                               return config;
+                       }
+               }
+#else
+               static AnonymousIdentificationSection Config = (AnonymousIdentificationSection) WebConfigurationManager.GetSection ("system.web/anonymousIdentification");
+#endif
        }
 }
 #endif