Shyju
Shyju

Reputation: 218702

ASP.NET How to Use SESSION and Cookies together?

I want to use session object in my web app.I want to store some cookies too(Some custom informations) .How can i use both without the URL not being modified like http://www.example.com/(S(lit3py55t21z5v55vlm25s55))/orderform.aspx

In my ASP.NET page, I am setting some session variable

Session["customerId"]="Some name";

Then i am trying to set some value in cookie

        HttpCookie objCookie = new HttpCookie("vendorName");
        Response.Cookies.Clear();
        Response.Cookies.Add(objCookie);
        objCookie.Values.Add(cookiename, "ATT");
        DateTime dtExpiry = DateTime.Now.AddDays(2);
        Response.Cookies[cookiename].Expires = dtExpiry;

In this page now i can access the sesion variable values,But when i m being Redirected to another asp.net page, I am not getting my session values there.Its seems like Its being lossed.

Any idea how to solve this. I want both session and cookies

Upvotes: 0

Views: 14053

Answers (4)

Priti Ranjan Dash
Priti Ranjan Dash

Reputation: 1

protected void btnSend_Click(object sender, EventArgs e)
    {
        // declaring a HttpCookies here with a Parameter
        HttpCookie Cookies = new HttpCookie("Name");

        //Clearing the Cookies
        Response.Cookies.Clear();

        // Set a value in it.
        // here the Cookies object act like a type and inside the'[" "]' is a       Cookie's  variable
        Cookies["ID"] = txtID.Text;
        Cookies["MyName"] = txtName.Text;
        Cookies["Contact_No"] = txtContactNo.Text;
        Cookies["EmailID"] = txtEmailID.Text;

        // Add it to the current web response.
        Response.Cookies.Add(Cookies);

        //Setting the Exparation Date For he Cookies
        HttpCookie Cookies1 = new HttpCookie("Expiration");
        Cookies1.Expires = DateTime.Now.AddDays(-1);

        //Redirecting to another page
        Response.Redirect("Accepting_Cookies_Details.aspx");
}

//Go the the target page where you want to retrieve your Cookies' values

protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie Cookie = Request.Cookies["Name"];
        string ID1 = Cookie["ID"];
        string MyName1 = Cookie["MyName"];
        string Contact_No1 = Cookie["Contact_No"];
        string EmailID1 = Cookie["EmailID"];
        Literal1.Text = ID1;
        Literal2.Text = MyName1;
        Literal3.Text = Contact_No1;
        Literal4.Text = EmailID1;
    }

Upvotes: 0

Ziad
Ziad

Reputation: 1036

You might consider using this little library:

http://www.codeproject.com/KB/aspnet/Univar.aspx

It can automatically switch to the session whenever the cookie is unavailable. It also has a server side implementation of the cookie whereby all cookies are stored on the server and asp.net authentification can be used to identify the user.

Upvotes: 0

Mark Arnott
Mark Arnott

Reputation: 1994

I think your problem may be this

Response.Cookies.Clear(); 

If you clear all the cookies, you will be clearing the cookie that ASP.Net uses to store the session identifier. Without that cookie ASP.Net can't hook up the users Session with subsequent requests and so the Session will be lost.

Upvotes: 1

Mark Arnott
Mark Arnott

Reputation: 1994

Cookies and Session variables are independent of each other. You may be confused because by default Asp.Net Sessions use a cookie to store the session identifier and when cookies are disabled, Asp.Net puts the session identifier in a query string value which is visible in the URL.

To use cookies do this

// Set the value in a response
Response.Cookies["SomeCookieVar"].Value = "SomethingImportant";

// After a post back read the value from the request
Request.Cookies["SomeCookieVar"].Value;

Session variables are accessed like this

// Set the value
Session["SomeSessionVar"] = "SomethingElse";
// Read the value
String SomeSetting = Session["SomeSessionVar"];

This is assuming that you are working in C# inside and ASPX page class. VB.Net has slightly different syntax and http handlers and modules require you to do some work to get to Request, Response and Session.

Both Session variables and Cookies values can be mixed and matched to your hearts content without any conflicts. One common scenario is to store values in cookies that you want to persist acrosss different sessions. But in order for that to work, you must set an expiration on your cookie. Cookies without expirations are non-persistent and will not last between browser sessions.

// make the cookie to last for a week
Request.Cookies["SomeCookieVar"].Expiration = DateTime.Now().AddDays(7);

Upvotes: 4

Related Questions