Hossein
Hossein

Reputation: 1768

Session State not Retained over calls to a Page Method

My server side code:

    [WebMethod(CacheDuration = 0, EnableSession = true)]
    public static int UserID()
    {
        if (HttpContext.Current.Session["UserID"] == null) return 0;
        int UserID = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
        return (UserID);

    }

My Client side code:

$.ajax({
    type: "POST", cache: false,
    url: "Login.aspx/UserID",
    data: "{'r':" + rnd() + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        alert(msg);
    }
});

This codes runs well on my localhost. and the "UserID" ajax call, return the right value of the Session parameter.

but when i try to upload my website on the server, the "UserID" ajax call always returns false!!

my server is asp.net 2.0 and I'm using jquery 1.3.2

So please help to solve this problem.

Upvotes: 1

Views: 2252

Answers (2)

Hossein
Hossein

Reputation: 1768

Read this article: ASP.NET Session State FAQ I found my answer in this questions:

  1. Q: Session states works on some web servers but not on others. A: Maybe machine name problem. See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q316112 .
  2. Q: Why are my Session variables lost frequently when using InProc mode? A: Probably because of application recycle. See http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316148

Changing sessionState mode to "StateServer" solved the problem. Use code below:

<sessionState mode="StateServer"
  stateConnectionString="tcpip=localhost:42424"
  cookieless="false"
  timeout="999"/>

Upvotes: 1

Salamander2007
Salamander2007

Reputation: 6394

A few things to check :

Are you using WebFarm configuration? there might be a problem when using session in WebFarm configuration.

Have you check your network traffic using tools like Fiddler or FireBug? Is ASP.NET Auth cookie sent during AJAX call?

Can you confirm your session var is valid?

Upvotes: 0

Related Questions