Search code examples
asp.netjqueryasmxpagemethods

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.


Solution

  • 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"/>