Example #1
0
        void OnEnter(object source, EventArgs eventArgs)
        {
            if (!Enabled)
            {
                return;
            }

            string anonymousID = null;

            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)
            {
                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;
        }
 private void OnEnter(object source, EventArgs eventArgs)
 {
     if (!s_Initialized)
     {
         Initialize();
     }
     if (s_Enabled)
     {
         bool flag2;
         HttpCookie cookie = null;
         bool flag = false;
         AnonymousIdData decodedValue = null;
         string cookieValue = null;
         bool isAuthenticated = false;
         HttpApplication application = (HttpApplication) source;
         HttpContext context = application.Context;
         isAuthenticated = context.Request.IsAuthenticated;
         if (isAuthenticated)
         {
             flag2 = CookielessHelperClass.UseCookieless(context, false, s_CookieMode);
         }
         else
         {
             flag2 = CookielessHelperClass.UseCookieless(context, true, s_CookieMode);
         }
         if ((s_RequireSSL && !context.Request.IsSecureConnection) && !flag2)
         {
             if (context.Request.Cookies[s_CookieName] != null)
             {
                 cookie = new HttpCookie(s_CookieName, string.Empty) {
                     HttpOnly = true,
                     Path = s_CookiePath,
                     Secure = s_RequireSSL
                 };
                 if (s_Domain != null)
                 {
                     cookie.Domain = s_Domain;
                 }
                 cookie.Expires = new DateTime(0x7cf, 10, 12);
                 if (context.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
                 {
                     cookie.Value = "NoCookie";
                 }
                 context.Response.Cookies.Add(cookie);
             }
         }
         else
         {
             if (!flag2)
             {
                 cookie = context.Request.Cookies[s_CookieName];
                 if (cookie != null)
                 {
                     cookieValue = cookie.Value;
                     cookie.Path = s_CookiePath;
                     if (s_Domain != null)
                     {
                         cookie.Domain = s_Domain;
                     }
                 }
             }
             else
             {
                 cookieValue = context.CookielessHelper.GetCookieValue('A');
             }
             decodedValue = GetDecodedValue(cookieValue);
             if ((decodedValue != null) && (decodedValue.AnonymousId != null))
             {
                 context.Request._AnonymousId = decodedValue.AnonymousId;
             }
             if (!isAuthenticated)
             {
                 if (context.Request._AnonymousId == null)
                 {
                     if (this._CreateNewIdEventHandler != null)
                     {
                         AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs(context);
                         this._CreateNewIdEventHandler(this, e);
                         context.Request._AnonymousId = e.AnonymousID;
                     }
                     if (string.IsNullOrEmpty(context.Request._AnonymousId))
                     {
                         context.Request._AnonymousId = Guid.NewGuid().ToString("D", CultureInfo.InvariantCulture);
                     }
                     else if (context.Request._AnonymousId.Length > 0x80)
                     {
                         throw new HttpException(System.Web.SR.GetString("Anonymous_id_too_long"));
                     }
                     if ((s_RequireSSL && !context.Request.IsSecureConnection) && !flag2)
                     {
                         return;
                     }
                     flag = true;
                 }
                 DateTime utcNow = DateTime.UtcNow;
                 if (!flag && s_SlidingExpiration)
                 {
                     if ((decodedValue == null) || (decodedValue.ExpireDate < utcNow))
                     {
                         flag = true;
                     }
                     else
                     {
                         TimeSpan span = (TimeSpan) (decodedValue.ExpireDate - utcNow);
                         if (span.TotalSeconds < ((s_CookieTimeout * 60) / 2))
                         {
                             flag = true;
                         }
                     }
                 }
                 if (flag)
                 {
                     DateTime dt = utcNow.AddMinutes((double) s_CookieTimeout);
                     cookieValue = GetEncodedValue(new AnonymousIdData(context.Request.AnonymousID, dt));
                     if (cookieValue.Length > 0x200)
                     {
                         throw new HttpException(System.Web.SR.GetString("Anonymous_id_too_long_2"));
                     }
                     if (!flag2)
                     {
                         cookie = new HttpCookie(s_CookieName, cookieValue) {
                             HttpOnly = true,
                             Expires = dt,
                             Path = s_CookiePath,
                             Secure = s_RequireSSL
                         };
                         if (s_Domain != null)
                         {
                             cookie.Domain = s_Domain;
                         }
                         context.Response.Cookies.Add(cookie);
                     }
                     else
                     {
                         context.CookielessHelper.SetCookieValue('A', cookieValue);
                         context.Response.Redirect(context.Request.RawUrl);
                     }
                 }
             }
         }
     }
 }
		void OnEnter (object source, EventArgs eventArgs)
		{
			if (!Enabled)
				return;

			string anonymousID = null;

			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) {
				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 void AnonymousIdentification_Creating(Object sender, AnonymousIdentificationEventArgs e)
 {
     e.AnonymousID = Guid.NewGuid().ToString();
 }
Example #5
0
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        private void OnEnter(Object source, EventArgs eventArgs)
        {
            if (!s_Initialized)
            {
                Initialize();
            }

            if (!s_Enabled)
            {
                return;
            }

            HttpApplication app;
            HttpContext     context;
            HttpCookie      cookie       = null;
            bool            createCookie = false;
            AnonymousIdData decodedValue = null;
            bool            cookieLess;
            string          encValue        = null;
            bool            isAuthenticated = false;

            app     = (HttpApplication)source;
            context = app.Context;

            isAuthenticated = context.Request.IsAuthenticated;

            if (isAuthenticated)
            {
                cookieLess = CookielessHelperClass.UseCookieless(context, false /* no redirect */, s_CookieMode);
            }
            else
            {
                cookieLess = CookielessHelperClass.UseCookieless(context, true /* do redirect */, s_CookieMode);
                //if (!cookieLess && s_RequireSSL && !context.Request.IsSecureConnection)
                //    throw new HttpException(SR.GetString(SR.Connection_not_secure_creating_secure_cookie));
            }

            ////////////////////////////////////////////////////////////////////////
            // Handle secure-cookies over non SSL
            if (s_RequireSSL && !context.Request.IsSecureConnection)
            {
                if (!cookieLess)
                {
                    cookie = context.Request.Cookies[s_CookieName];
                    if (cookie != null)
                    {
                        cookie          = new HttpCookie(s_CookieName, String.Empty);
                        cookie.HttpOnly = true;
                        cookie.Path     = s_CookiePath;
                        cookie.Secure   = s_RequireSSL;
                        if (s_Domain != null)
                        {
                            cookie.Domain = s_Domain;
                        }
                        cookie.Expires = new System.DateTime(1999, 10, 12);

                        if (context.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
                        {
                            cookie.Value = "NoCookie";
                        }
                        context.Response.Cookies.Add(cookie);
                    }

                    return;
                }
            }


            ////////////////////////////////////////////////////////////
            // Step 2: See if cookie, or cookie-header has the value
            if (!cookieLess)
            {
                cookie = context.Request.Cookies[s_CookieName];
                if (cookie != null)
                {
                    encValue    = cookie.Value;
                    cookie.Path = s_CookiePath;
                    if (s_Domain != null)
                    {
                        cookie.Domain = s_Domain;
                    }
                }
            }
            else
            {
                encValue = context.CookielessHelper.GetCookieValue('A');
            }

            decodedValue = GetDecodedValue(encValue);

            if (decodedValue != null && decodedValue.AnonymousId != null)
            {
                // Copy existing value in Request
                context.Request.AnonymousID = decodedValue.AnonymousId;
            }
            if (isAuthenticated) // For the authenticated case, we are done
            {
                return;
            }

            if (context.Request.AnonymousID == null)
            {
                ////////////////////////////////////////////////////////////
                // Step 3: Create new Identity

                // Raise event
                if (_CreateNewIdEventHandler != null)
                {
                    AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs(context);
                    _CreateNewIdEventHandler(this, e);
                    context.Request.AnonymousID = e.AnonymousID;
                }

                // Create from GUID
                if (string.IsNullOrEmpty(context.Request.AnonymousID))
                {
                    context.Request.AnonymousID = Guid.NewGuid().ToString("D", CultureInfo.InvariantCulture);
                }
                else
                {
                    if (context.Request.AnonymousID.Length > MAX_ID_LENGTH)
                    {
                        throw new HttpException(SR.GetString(SR.Anonymous_id_too_long));
                    }
                }
                if (s_RequireSSL && !context.Request.IsSecureConnection && !cookieLess)
                {
                    return; // Don't create secure-cookie in un-secured connection
                }
                createCookie = true;
            }

            ////////////////////////////////////////////////////////////
            // Step 4: Check if cookie has to be created
            DateTime dtNow = DateTime.UtcNow;

            if (!createCookie)
            {
                if (s_SlidingExpiration)
                {
                    if (decodedValue == null || decodedValue.ExpireDate < dtNow)
                    {
                        createCookie = true;
                    }
                    else
                    {
                        double secondsLeft = (decodedValue.ExpireDate - dtNow).TotalSeconds;
                        if (secondsLeft < (double)((s_CookieTimeout * 60) / 2))
                        {
                            createCookie = true;
                        }
                    }
                }
            }

            ////////////////////////////////////////////////////////////
            // Step 4: Create new cookie or cookieless header
            if (createCookie)
            {
                DateTime dtExpireTime = dtNow.AddMinutes(s_CookieTimeout);
                encValue = GetEncodedValue(new AnonymousIdData(context.Request.AnonymousID, dtExpireTime));
                if (encValue.Length > MAX_ENCODED_COOKIE_STRING)
                {
                    throw new HttpException(SR.GetString(SR.Anonymous_id_too_long_2));
                }

                if (!cookieLess)
                {
                    cookie          = new HttpCookie(s_CookieName, encValue);
                    cookie.HttpOnly = true;
                    cookie.Expires  = dtExpireTime;
                    cookie.Path     = s_CookiePath;
                    cookie.Secure   = s_RequireSSL;
                    if (s_Domain != null)
                    {
                        cookie.Domain = s_Domain;
                    }
                    context.Response.Cookies.Add(cookie);
                }
                else
                {
                    context.CookielessHelper.SetCookieValue('A', encValue);
                    context.Response.Redirect(context.Request.RawUrl);
                }
            }
        }
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        private void OnEnter(Object source, EventArgs eventArgs) {
            if (!s_Initialized)
                Initialize();

            if (!s_Enabled)
                return;

            HttpApplication  app;
            HttpContext      context;
            HttpCookie       cookie = null;
            bool             createCookie = false;
            AnonymousIdData  decodedValue = null;
            bool             cookieLess;
            string           encValue = null;
            bool             isAuthenticated = false;

            app = (HttpApplication)source;
            context = app.Context;

            isAuthenticated = context.Request.IsAuthenticated;

            if (isAuthenticated) {
                cookieLess = CookielessHelperClass.UseCookieless(context, false /* no redirect */, s_CookieMode);
            } else {
                cookieLess = CookielessHelperClass.UseCookieless(context, true /* do redirect */, s_CookieMode);
                //if (!cookieLess && s_RequireSSL && !context.Request.IsSecureConnection)
                //    throw new HttpException(SR.GetString(SR.Connection_not_secure_creating_secure_cookie));
            }

            ////////////////////////////////////////////////////////////////////////
            // Handle secure-cookies over non SSL
            if (s_RequireSSL && !context.Request.IsSecureConnection)
            {
                if (!cookieLess)
                {
                    cookie = context.Request.Cookies[s_CookieName];
                    if (cookie != null)
                    {
                        cookie = new HttpCookie(s_CookieName, String.Empty);
                        cookie.HttpOnly = true;
                        cookie.Path = s_CookiePath;
                        cookie.Secure = s_RequireSSL;
                        if (s_Domain != null)
                            cookie.Domain = s_Domain;
                        cookie.Expires = new System.DateTime(1999, 10, 12);

                        if (context.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
                            cookie.Value = "NoCookie";
                        context.Response.Cookies.Add(cookie);
                    }

                    return;
                }
            }


            ////////////////////////////////////////////////////////////
            // Step 2: See if cookie, or cookie-header has the value
            if (!cookieLess)
            {
                cookie = context.Request.Cookies[s_CookieName];
                if (cookie != null)
                {
                    encValue = cookie.Value;
                    cookie.Path = s_CookiePath;
                    if (s_Domain != null)
                        cookie.Domain = s_Domain;
                }
            }
            else
            {
                encValue = context.CookielessHelper.GetCookieValue('A');
            }

            decodedValue = GetDecodedValue(encValue);

            if (decodedValue != null && decodedValue.AnonymousId != null) {
                // Copy existing value in Request
                context.Request.AnonymousID = decodedValue.AnonymousId;
            }
            if (isAuthenticated) // For the authenticated case, we are done
                return;

            if (context.Request.AnonymousID == null) {
                ////////////////////////////////////////////////////////////
                // Step 3: Create new Identity

                // Raise event
                if (_CreateNewIdEventHandler != null) {
                    AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs(context);
                    _CreateNewIdEventHandler(this, e);
                    context.Request.AnonymousID = e.AnonymousID;
                }

                // Create from GUID
                if (string.IsNullOrEmpty(context.Request.AnonymousID)) {
                    context.Request.AnonymousID = Guid.NewGuid().ToString("D", CultureInfo.InvariantCulture);
                } else {
                    if (context.Request.AnonymousID.Length > MAX_ID_LENGTH)
                        throw new HttpException(SR.GetString(SR.Anonymous_id_too_long));
                }
                if (s_RequireSSL && !context.Request.IsSecureConnection && !cookieLess)
                    return; // Don't create secure-cookie in un-secured connection

                createCookie = true;
            }

            ////////////////////////////////////////////////////////////
            // Step 4: Check if cookie has to be created
            DateTime dtNow = DateTime.UtcNow;
            if (!createCookie) {
                if (s_SlidingExpiration) {
                    if (decodedValue == null || decodedValue.ExpireDate < dtNow) {
                        createCookie = true;
                    } else {
                        double secondsLeft =  (decodedValue.ExpireDate - dtNow).TotalSeconds;
                        if (secondsLeft < (double) ((s_CookieTimeout*60)/2)) {
                            createCookie = true;
                        }
                    }
                }
            }

            ////////////////////////////////////////////////////////////
            // Step 4: Create new cookie or cookieless header
            if (createCookie) {
                DateTime dtExpireTime = dtNow.AddMinutes(s_CookieTimeout);
                encValue = GetEncodedValue(new AnonymousIdData(context.Request.AnonymousID, dtExpireTime));
                if (encValue.Length > MAX_ENCODED_COOKIE_STRING)
                    throw new HttpException(SR.GetString(SR.Anonymous_id_too_long_2));

                if (!cookieLess) {
                    cookie          = new HttpCookie(s_CookieName, encValue);
                    cookie.HttpOnly = true;
                    cookie.Expires  = dtExpireTime;
                    cookie.Path     = s_CookiePath;
                    cookie.Secure   = s_RequireSSL;
                    if (s_Domain != null)
                        cookie.Domain   = s_Domain;
                    context.Response.Cookies.Add(cookie);
                } else {
                    context.CookielessHelper.SetCookieValue('A', encValue);
                    context.Response.Redirect(context.Request.RawUrl);
                }
            }
        }
 private void OnEnter(object source, EventArgs eventArgs)
 {
     if (!s_Initialized)
     {
         Initialize();
     }
     if (s_Enabled)
     {
         bool            flag2;
         HttpCookie      cookie          = null;
         bool            flag            = false;
         AnonymousIdData decodedValue    = null;
         string          cookieValue     = null;
         bool            isAuthenticated = false;
         HttpApplication application     = (HttpApplication)source;
         HttpContext     context         = application.Context;
         isAuthenticated = context.Request.IsAuthenticated;
         if (isAuthenticated)
         {
             flag2 = CookielessHelperClass.UseCookieless(context, false, s_CookieMode);
         }
         else
         {
             flag2 = CookielessHelperClass.UseCookieless(context, true, s_CookieMode);
         }
         if ((s_RequireSSL && !context.Request.IsSecureConnection) && !flag2)
         {
             if (context.Request.Cookies[s_CookieName] != null)
             {
                 cookie = new HttpCookie(s_CookieName, string.Empty)
                 {
                     HttpOnly = true,
                     Path     = s_CookiePath,
                     Secure   = s_RequireSSL
                 };
                 if (s_Domain != null)
                 {
                     cookie.Domain = s_Domain;
                 }
                 cookie.Expires = new DateTime(0x7cf, 10, 12);
                 if (context.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
                 {
                     cookie.Value = "NoCookie";
                 }
                 context.Response.Cookies.Add(cookie);
             }
         }
         else
         {
             if (!flag2)
             {
                 cookie = context.Request.Cookies[s_CookieName];
                 if (cookie != null)
                 {
                     cookieValue = cookie.Value;
                     cookie.Path = s_CookiePath;
                     if (s_Domain != null)
                     {
                         cookie.Domain = s_Domain;
                     }
                 }
             }
             else
             {
                 cookieValue = context.CookielessHelper.GetCookieValue('A');
             }
             decodedValue = GetDecodedValue(cookieValue);
             if ((decodedValue != null) && (decodedValue.AnonymousId != null))
             {
                 context.Request._AnonymousId = decodedValue.AnonymousId;
             }
             if (!isAuthenticated)
             {
                 if (context.Request._AnonymousId == null)
                 {
                     if (this._CreateNewIdEventHandler != null)
                     {
                         AnonymousIdentificationEventArgs e = new AnonymousIdentificationEventArgs(context);
                         this._CreateNewIdEventHandler(this, e);
                         context.Request._AnonymousId = e.AnonymousID;
                     }
                     if (string.IsNullOrEmpty(context.Request._AnonymousId))
                     {
                         context.Request._AnonymousId = Guid.NewGuid().ToString("D", CultureInfo.InvariantCulture);
                     }
                     else if (context.Request._AnonymousId.Length > 0x80)
                     {
                         throw new HttpException(System.Web.SR.GetString("Anonymous_id_too_long"));
                     }
                     if ((s_RequireSSL && !context.Request.IsSecureConnection) && !flag2)
                     {
                         return;
                     }
                     flag = true;
                 }
                 DateTime utcNow = DateTime.UtcNow;
                 if (!flag && s_SlidingExpiration)
                 {
                     if ((decodedValue == null) || (decodedValue.ExpireDate < utcNow))
                     {
                         flag = true;
                     }
                     else
                     {
                         TimeSpan span = (TimeSpan)(decodedValue.ExpireDate - utcNow);
                         if (span.TotalSeconds < ((s_CookieTimeout * 60) / 2))
                         {
                             flag = true;
                         }
                     }
                 }
                 if (flag)
                 {
                     DateTime dt = utcNow.AddMinutes((double)s_CookieTimeout);
                     cookieValue = GetEncodedValue(new AnonymousIdData(context.Request.AnonymousID, dt));
                     if (cookieValue.Length > 0x200)
                     {
                         throw new HttpException(System.Web.SR.GetString("Anonymous_id_too_long_2"));
                     }
                     if (!flag2)
                     {
                         cookie = new HttpCookie(s_CookieName, cookieValue)
                         {
                             HttpOnly = true,
                             Expires  = dt,
                             Path     = s_CookiePath,
                             Secure   = s_RequireSSL
                         };
                         if (s_Domain != null)
                         {
                             cookie.Domain = s_Domain;
                         }
                         context.Response.Cookies.Add(cookie);
                     }
                     else
                     {
                         context.CookielessHelper.SetCookieValue('A', cookieValue);
                         context.Response.Redirect(context.Request.RawUrl);
                     }
                 }
             }
         }
     }
 }