Cookies can't read.

Last post 05-14-2008 6:06 AM by Benson Yu - MSFT. 4 replies.

Sort Posts:

  • Cookies can't read.

    05-08-2008, 8:45 AM
    • Loading...
    • durai_krisna
    • Joined on 02-14-2008, 4:22 AM
    • Madurai,India
    • Posts 125

    Hi friends,

    I am using cookies in my project.I wan to read cookies in global.asax page.

    i use following code in session_end method.

     Session["user"] = Request.Cookies["userinfo"]["user"].ToString();
                Session["role"] = Request.Cookies["userinfo"]["role"].ToString();
                Session["userphoto"] = Request.Cookies["userinfo"]["userphoto"].ToString();

    but "Request is not available in this context" error occur.

    how to get cookies values in global.asax?

    Thanks,

    Durai. 

  • Re: Cookies can't read.

    05-08-2008, 9:03 AM
    • Loading...
    • kamii47
    • Joined on 05-26-2005, 4:04 PM
    • Karachi, Pakistan
    • Posts 1,533

    check session prior

    if (HttpContext.Current.Session != null)
    {

    Kamran Shahid(MCSD.NET,MCPD.net[web])
    Sr. Software Engineer
    Netprosys Inc.
    www.netprosys.com

    Remember to click "Mark as Answer" on the post that helps U
  • Re: Cookies can't read.

    05-08-2008, 1:34 PM
    • Loading...
    • Haissam
    • Joined on 10-05-2006, 6:25 AM
    • Beirut - Lebanon
    • Posts 4,553

    You can to use HttpContext.Current.Request.Cookies under System.Web namepsace

     

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: Cookies can't read.

    05-09-2008, 12:02 AM
    • Loading...
    • durai_krisna
    • Joined on 02-14-2008, 4:22 AM
    • Madurai,India
    • Posts 125

     hi haissam,

     i am using cookies object in login page using following code.

      HttpCookie aCookie = new HttpCookie("userInfo");
                            aCookie.Values["userid"] = dsuser.Tables[0].Rows[0][0].ToString();
                            aCookie.Values["user"] = dsuser.Tables[0].Rows[0][1].ToString();
                            aCookie.Values["role"] = dsuser.Tables[0].Rows[0][3].ToString();

     Response.Cookies.Add(aCookie); 

     

    but i want these cookies in global.asax.how to read?code plz

    Thanks,

    Durai 

  • Re: Cookies can't read.

    05-14-2008, 6:06 AM
    Answer

    durai_krisna:

    i am using cookies object in login page using following code.

      HttpCookie aCookie = new HttpCookie("userInfo");
                            aCookie.Values["userid"] = dsuser.Tables[0].Rows[0][0].ToString();
                            aCookie.Values["user"] = dsuser.Tables[0].Rows[0][1].ToString();
                            aCookie.Values["role"] = dsuser.Tables[0].Rows[0][3].ToString();

     Response.Cookies.Add(aCookie); 

    but i want these cookies in global.asax.how to read?code plz

    durai_krisna:

    I am using cookies in my project.I wan to read cookies in global.asax page.
    i use following code in session_end method.

    Session["user"] = Request.Cookies["userinfo"]["user"].ToString();
                Session["role"] = Request.Cookies["userinfo"]["role"].ToString();
                Session["userphoto"] = Request.Cookies["userinfo"]["userphoto"].ToString();

    but "Request is not available in this context" error occur.


    Hi durai_krisna,

    In the Session_End event hander, the session objects for current user is going to be terminated. Therefore, assign values to session at this time does not make sense.

    Anyway, since your concern is how to read cookie in the Global.asax file, I post the following code for your reference. Hope it is helpful to you.

    ********** Global.asax ***********
            protected void Application_AcquireRequestState(object sender, EventArgs e)
            {
                if (Request.Cookies["userinfo"] != null)
                {
                    HttpContext.Current.Session["userName"] = Request.Cookies["userinfo"]["user"].ToString();
                }
            }

    ********** login.aspx.cs ************
         HttpCookie aCookie = new HttpCookie("userInfo");
         aCookie.Values["user"] = "My Name";
         Response.Cookies.Add(aCookie);

    ********** default.aspx.cs *************
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["userName"] != null)
                {
                    Label1.Text = Session["userName"].ToString();
                }
            }

    Sincerely,
    Benson Yu
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
Page 1 of 1 (5 items)