asn1981
asn1981

Reputation: 237

c# How to get session from asp.net session cookie

I'm pretty basic with .net

But basically I've been told that to have session stickiness for my website in the environment it is to be deployed means I have to get session from the cookie ASP.NET_SessionId

But what does this mean/how do I use this?

And where I am using my existing session code e.g. Session.Add("Something", something) do I now need to change this?

Upvotes: 0

Views: 14494

Answers (5)

The Muffin Man
The Muffin Man

Reputation: 20004

// Set
Session["mySession"] = "Hello World";

// Get
var s = Session["mySession"];

Upvotes: 0

Robert Koritnik
Robert Koritnik

Reputation: 104999

This is automated for you

You don't have to manually read cookies yourself. Asp.net does it for you. So whenever you access Session dictionary your session will already be preserved if it existed from your previous request(s). If there is none (or expired) it will also be automatically created so adding items to it will make it work without a problem.

So basically instead of accessing Session identifier from a cookies and do whatever with it (as you won't be able to access intrinsic session store), just use Session:

Session["SomeKey"] = myVar;
// or
Session.Add("SomeKey", myVar);

and for reading

var o = Session["SomeKey"];

Are cookies mandatory?

Basically Asp.net supports other means of session ID storage apart from cookies. You can enable cookieless session if you wanted to (a setting in web.config file). Session identifier will be added to your URL like:

http://www.example.com/(S(lit3py55t21z5v55vlm25s55))/orderform.aspx

Read more about it here on MSDN.

What's the default?

By default session identifier is preserved in cookies. That means that you don't have to change anything in web.config. But if you'd like to use other means you'd have to set that in web.config (check the same upper link as before).

Upvotes: 1

Ryan Emerle
Ryan Emerle

Reputation: 15811

Session stickiness is not a feature of ASP.net, but rather of a load balancer that may sit in front of it. If you are using InProc sessions, then the session data is stored in the server's memory. With "sticky" sessions, the load balancer will send all requests from the same source (based on IP usually) to the same server to ensure that the session always exists.

This isn't the most scalable way to handle a web farm scenario. Microsoft introduced two other mechanisms, StateServer and SqlServer which serve to send the session data to a single location for all web front ends.

You likely just need to make sure you don't have cookieless set to true in your configuration.

Regardless of how this is all configured, you will always retrieve your session data the same way - ASP.NET abstracts the details away.

Upvotes: 0

thekip
thekip

Reputation: 3760

You can get a reference to the current session in multiple ways but the most easy way is just use the session property on the page.

See: http://msdn.microsoft.com/en-us/library/system.web.ui.page.session.aspx

Upvotes: 0

Nate
Nate

Reputation: 30636

Typically the way that I use session variables in ASP.NET is like this

// set
Session["SessionVariableName"] = localVariable;

// get
localVariable = Session["SessionVariableName"] as LocalType; // then check nulls

The issue with any sessions is that if you don't go out of your way to change it, the default stores sessions in-proc so every time IIS recycles a worker process your session is lost. This can be easily fixed by using the built-in ASP.NET State Service. A quick search turned up this article on using it.

Upvotes: 0

Related Questions