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();
}
}