Răsfoiți Sursa

* AnonymousIdentificationModule.cs: fixed anonymous id cookie

svn path=/trunk/mcs/; revision=67375
Vladimir Krasnov 19 ani în urmă
părinte
comite
a30d31752c

+ 38 - 15
mcs/class/System.Web/System.Web.Security/AnonymousIdentificationModule.cs

@@ -30,22 +30,24 @@
 
 #if NET_2_0
 
+using System;
 using System.Web;
 using System.Web.Configuration;
+using System.Text;
 
 namespace System.Web.Security {
 
 	public sealed class AnonymousIdentificationModule : IHttpModule {
 
+		HttpApplication app;
 		public event AnonymousIdentificationEventHandler Creating;
-		
+
 		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 +63,7 @@ namespace System.Web.Security {
 			app.PostAuthenticateRequest += OnEnter;
 		}
 
+		[MonoTODO ("cookieless userid")]
 		void OnEnter (object source, EventArgs eventArgs)
 		{
 			if (!Enabled)
@@ -68,27 +71,47 @@ namespace System.Web.Security {
 
 			string anonymousID = null;
 
-			if (Creating != null) {
-				AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs (HttpContext.Current);
-				Creating (this, e);
+			HttpCookie cookie = app.Request.Cookies [Config.CookieName];
+			if (cookie == null || (cookie.Expires != DateTime.MinValue && cookie.Expires < DateTime.Now)) {
+				if (Creating != null) {
+					AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs (HttpContext.Current);
+					Creating (this, e);
 
-				anonymousID = e.AnonymousID;
-			}
+					anonymousID = e.AnonymousID;
+				}
+
+				if (anonymousID == null)
+					anonymousID = Guid.NewGuid ().ToString ();
 
-			if (anonymousID == null)
-				anonymousID = Guid.NewGuid().ToString();
+				app.Request.AnonymousID = anonymousID;
 
-			app.Request.AnonymousID = anonymousID;
+				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);
+			}
+			else {
+				app.Request.AnonymousID = Encoding.Unicode.GetString (Convert.FromBase64String (cookie.Value));
+			}
 		}
 
 		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;
+		static AnonymousIdentificationSection Config
+		{
+			get
+			{
+				return (AnonymousIdentificationSection) WebConfigurationManager.GetSection ("system.web/anonymousIdentification");
+			}
+		}
 	}
 }
 #endif

+ 4 - 0
mcs/class/System.Web/System.Web.Security/ChangeLog

@@ -1,3 +1,7 @@
+2006-11-05 Vladimir Krasnov <[email protected]>
+
+	* AnonymousIdentificationModule.cs: fixed anonymous id cookie
+
 2006-11-05 Vladimir Krasnov <[email protected]>
 
 	* MembershipProvider.cs, SqlMembershipProvider.cs: