Tuesday, April 16, 2013

Maintaining Cookies Variable in DLL in Asp.Net

I have mentioned about Maintaining Session Variable in DLL in Asp.net in this blog before and this is something similar to that session post.
Let me have a brief explanation of this post.
As we know, we usually see a check box saying remember me or keep logged in most of the web sites log in page.
Thus, we have to play cookie in user's browser with these functions Read, Write, Clear cookies.
Imagine how messy if we create/write/clear cookie inside the code in every time of creating/writing/clearing cookies.
In such case, how if we create CookieHelper class having Read/Write/Clearing only one time.
The example mechanism code snippet of keep me logged in is as below.

namespace MyCookieHouse
{
    public enum COOKIE_NAME
    {
        CK_KEEPLOGIN, // login
        USERINFO_USERID
    }

    [Serializable()]
    public class MyCookieHelper
    {
        public static string USER_ID //login user id OR Shipping Agent Code
        {
            get
            {
                return GetCookieValue(COOKIE_NAME.CK_KEEPLOGIN, COOKIE_NAME.USERINFO_USERID);
            }
            set
            {
                SetCookieValue(COOKIE_NAME.CK_KEEPLOGIN, COOKIE_NAME.USERINFO_USERID, value);
            }
        }
  
        public CurrentCOOKIE()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static bool IsKeepLogin()
        {
            HttpCookie ck = System.Web.HttpContext.Current.Request.Cookies[COOKIE_NAME.CK_KEEPLOGIN.ToString()];
            if (ck != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void ClearCookie(string CookieName)
        {
            HttpCookie ck = System.Web.HttpContext.Current.Request.Cookies[CookieName];
            if (ck != null)
            {
                ck.Expires = DateTime.Now.AddDays(-1d);
                System.Web.HttpContext.Current.Response.Cookies.Add(ck);
            }
        }

        private static string GetCookieValue(COOKIE_NAME CK_NAME, COOKIE_NAME CK_NAME_KEY)
        {
            HttpCookie ck = System.Web.HttpContext.Current.Request.Cookies[CK_NAME.ToString()];
            if (ck != null)
            {
                return ck[CK_NAME_KEY.ToString()];
            }
            else
            {
                return null;
            }
        }

        private static void SetCookieValue(COOKIE_NAME CK_NAME, COOKIE_NAME CK_NAME_KEY, string COOKIEVALUE)
        {
            HttpCookie ck = System.Web.HttpContext.Current.Request.Cookies[CK_NAME.ToString()];
            if (ck != null)
            {
                ck[CK_NAME_KEY.ToString()] = COOKIEVALUE;
                System.Web.HttpContext.Current.Response.Cookies.Add(ck);
            }
            else
            {
                ck = new HttpCookie(CK_NAME.ToString());
                ck[CK_NAME_KEY.ToString()] = COOKIEVALUE;
                ck.Expires = DateTime.Now.AddYears(5);
                System.Web.HttpContext.Current.Response.Cookies.Add(ck);
            }
        }

    }
}

This is something about sharing knowledge and appreciate your comment if you have some better way.
Million thanks. Enjoy! :)

No comments:

Post a Comment