I'm facing an issue where the system is generating a new session for each user when I run an ajax query, instead of having just 1 session per user. Strangely, there were no issues when I tested it in debug mode on my computer, but problems arose on the client site in the distributed application.
Here is the ASP page code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Text1" type="text" /><br />
<asp:Button ID="btn_ASP" runat="server" onclick="btn_ASP_Click" Text="Asp" />
<input id="btn_JS" type="button" value="JS" onclick="GetUsersList();" /><br />
The ASP button responsible for generating the 'ConnectCount' Session element:
protected void btn_ASP_Click(object sender, EventArgs e)
{
int count;
if (HttpContext.Current.Session["ConnectCount"] == null)
count = 1;
else
count = (int)HttpContext.Current.Session["ConnectCount"] + 1;
HttpContext.Current.Session["ConnectCount"] = count;
TextBox1.Text = count.ToString();
}
Code in the Web Service:
[WebMethod(EnableSession = true)]
[ScriptMethod]
public string Get_UsersList2()
{
int count;
if (HttpContext.Current.Session["ConnectCount"] == null)
count = 1;
else
count = (int)HttpContext.Current.Session["ConnectCount"] + 1;
HttpContext.Current.Session["ConnectCount"] = count;
return count.ToString();
}
Ajax query to the Web Service:
function GetUsersList() {
$.ajax({
url: '/WbSrv_ACTR.asmx/Get_UsersList2',
type: "POST",
contentType: "application/json; charset=utf-8",
data: '{}',
success: function (data, st) {
if (st == "success") {
if (data.d != "") {
text1.value = data.d;
}
}
},
error: function () {
alert("Cannot get list of users");
}
});
}
When I conducted the same operations on a server running IIS 7.5:
result: asp = 5, jq = 1 (incorrect)
The server seems to be using a different session, as elements added via the ASP button are missing in the Web Service. However, the SessionID remains the same.
Does anyone have a solution or workaround for this issue?
Web config:
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<customErrors mode="Off" />
<authorization>
<deny users="?" />
</authorization>
</system.web>
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>