views:

525

answers:

2

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.

A: 

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?

Salamander2007
What is the WebFarm? my internet hosting provider is : ixwebhosting(Business Plus (Win) Plan)
desmati
Yes. I'm using fireBug to monitor the traffic. how can I check if Auth cookie is sent?
desmati
as I said, I'm sure that my session vars are set. the log in form will set the UserID parameter equals to an int>0 on successful log in.
desmati
Locate your AJAX HTTP Post request, and examine the header. The cookie is in the header. You have to activate Net panel to be able to inspect traffic.
Salamander2007
I think the cookie is sent : ASP.NET_SessionId=waqb13z5ormwcizqdzhzys56;
desmati
Salamandar2007 thank you for the answers. can You give me some ideas about checking WebFarm Configuration? I only know that my session is InProc
desmati
My Headers:Host: www.???.comUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 (.NET CLR 3.5.30729)Accept: application/json, text/javascript, */*Accept-Language: en-us,en;q=0.5Accept-Encoding: gzip,deflateAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7Keep-Alive: 300Connection: keep-aliveContent-Type: application/json; charset=utf-8X-Requested-With: XMLHttpRequestContent-Length: 112Cookie: ASP.NET_SessionId=waqb13z8qrmwcizqdzhzjchs6; undefined=1; remember=1; email=9725???; password=ae8???Pragma: no-cacheCache-Control: no-cache
desmati
+1  A: 

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