views:

125

answers:

2

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");`
+2  A: 

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

CSharpAtl
I like this answer but klabranche took the time to link cookieless session
acidzombie24
+3  A: 

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

klabranche
+1..good call if you dont mind the id visible
CSharpAtl
I agree @CSharpAtl, definitely a less than pretty solution.
klabranche