user34537
user34537

Reputation:

Session seem to not work asp.net

I am not sure why this happens. In the same program i am using sessions which has worked. Now i am trying to make my site compatible when the user does not send cookies (which should be simple). I wrote

long userId, loginId;
//...
//put data into cookies
HttpContext.Current.Session.Add("userId", userId.ToString());
HttpContext.Current.Session.Add("loginId", loginId.ToString());

and i see that they are null in this statement

var cookies = HttpContext.Current.Request.Cookies;
long mUserId;
string u, id;
if (cookies["userId"] == null)
{
    //these are null
    u = (string)HttpContext.Current.Session["userId"];
    id = (string)HttpContext.Current.Session["loginId"];
}

In both cases (working and not working) after i set the session i call

HttpContext.Current.Response.Redirect("someUrl");`

Upvotes: 0

Views: 1248

Answers (2)

Kevin LaBranche
Kevin LaBranche

Reputation: 21078

As mentioned by CSharpAtl this is by design:

However, in the web.config if you set the session to be cookieless this should work BUT it will add a session id to the url.

Here's an article from MSDN on this: http://msdn.microsoft.com/en-us/library/aa479314.aspx

Upvotes: 3

CSharpAtl
CSharpAtl

Reputation: 7512

if the client does not accept cookies, SessionState cannot be used because there is a key to the Session state that is stored in a cookie that must be used to retrieve the Session state on postback

Upvotes: 2

Related Questions