I have a page which allow user to login.
Once they are login, a cookie will create with a value and redirect the user to specify pages..

And a master page will be load. Inside the master page will retrieve the cookie value and display if the cookie value not null.
and also provided a get method which retrieve the cookie value.
But in every child pages i do have link to this master page Master.getCookieValue
Now my problem is that any way/trick is able to do one checking of cookie value in master page only instead of doing the checking in every single child page?
If my explanation not very clear..
Code Below

Login.aspx
protected void SetDestinationURL(object sender, EventArgs e)
{
    if (loginMember() || loginManager())
    {
        HttpCookie UsernameCookies = new HttpCookie("Username");
        UsernameCookies.Value = txtUsername.Text;
        UsernameCookies.Expires = DateTime.Now.AddHours(1);
        Response.Cookies.Add(UsernameCookies);
        BasePage.Alert.Show("Please proceed to Profile Modification to update your Monthly Budget & Saving Rate");                
    }
    if (loginMember())
        Response.Redirect("~/Member/Home.aspx");
    else if (loginManager())
        Response.Redirect("~/Manager/Home.aspx");
    else
        BasePage.Alert.Show("Unauthorized User Access");
}

In Member/Home.aspx which loaded a master page Member.Master

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Cookies["Username"] != null)
    {
        hlUsername.Text = Request.Cookies["Username"].Value;
        showProfilePicture();
    }
    else
        Response.Redirect("~/ErrorPages/401.aspx");            
}

public string getUsername
{
    get { return Request.Cookies["Username"].Value; }
}

In every child pages which linked this master must to get the cookie value for some purpose.
Of course, if the cookie is expired, every child pages cannot be access, thus i do the cookie checking in every child pages.

protected void Page_Load(object sender, EventArgs e)
{
    if(Request.Cookies["Username"] != null)
        Response.Write(Master.getUsername);
    else
        Response.Redirect("ErrorPages.aspx");
}

So is that any way to do the cookies value checking instead of checking in all child pages?

Recommended Answers

All 9 Replies

You should have to create wrapper class to manage cookies and session data.

For example,

public class CookieWrapper
    {
       /* Username property */
        public static string Username
        {
            get
            {
                return GetCookie("Username");
            }
            set
            {
                SetCookie("Username", value, false);
            }
        }
        /* Role property */
        public static string Role
        {
            get
            {
                return GetCookie("Role");
            }
            set
            {
                SetCookie("Role", value,false);
            }
        }

        /* Create a cookie */
        public static  void SetCookie(string key, string value,bool persist)
        {
            HttpCookie cookie = new HttpCookie(key, value);
            if (persist)
                cookie.Expires = DateTime.Now.AddDays(15);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /* Read a cookie */
        public static  string  GetCookie(string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie == null)
                return "";
            return cookie.Value;
        }
        /* Remove a cookie */
        public static void RemoveCookie(string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            cookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }

And to set Username (cookie)

CookieWrapper.Username = "user1";

just create another new aspx page to apply these method?

Not an aspx page... Right click project name, add item, class. A new .cs file will be added to your App_Code folder.

HttpCookie cookie = new HttpCookie(key, value);

In this line is it means that the key cookies is able to store multiple values?

A cookie can store subkeys but I haven't seen the syntax you are presenting...

For setting up a cookie with name value pairs, look at the section "Cookies with more than one value" - ASP.NET Cookies Overview

is possible to store multiple value with the same key?

I am not exactly sure what you mean by your question, but you can store multiple name/value pairs in a single cookie. for example...

HttpCookie myCookie = new HttpCookie("userInfo");
myCookie.Values["userName"] = "user1";
myCookie.Values["lastVisit"] = DateTime.Now.ToString();

Retreive the cookie...

myCookieStringVar = Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);

How about if like this

HttpCookie myCookie = new HttpCookie("userInfo");
myCookie.Values = "user1";
myCookie.Values = "user2";
        public static string Username
        {
            get
            {
                return GetCookie("Username");
            }
            set
            {
                SetCookie("Username", value, false);
            }

In this code i am knowing the "Username" is fixed for the Cookies key so when i type
CookieWrapper.Username = "1"; Which mean i am creating a new cookie key = "Username" and value=1. Am i right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.