IIS7会话失去了它的价值

Ste*_*rov 6 c# asp.net asp.net-ajax session-variables

我已经将挑战响应方案实现为Ajax处理程序.出于某种原因,它在工作几个月后停止工作.调查问题表明,Context.Session[KEY]在挑战和响应呼叫之间失去了价值.

我把Session_StartSession_End(和其他几个)在Global.asax.cs中的方法与一些记录那里,我看到一个新的在session_start事件与相同的会话ID解雇,没有Session_End中的事件

问题是:为什么IIS会丢失会话值?

更新:我尝试切换到SQLServer会话,但行为没有变化.在极少数情况下,会话按预期工作,不确定原因.我尝试了所有"会话丢失变量"故障排除指南我发现无效

更新2:我将问题缩小到缺少会话cookie,但修改my.browsers配置后几次尝试都没有解决问题.当我从浏览器调用ajax处理程序时,会话cookie"ASP.NetSessionId"按预期显示.我将站点和服务器的IIS设置中的cookie名称更改为"SessionId",但即使重新启动服务器,我仍然看到ASP.NET.我仍然想把这个赏金给予那些知道发生了什么的人.同时我通过在代码中设置会话cookie来解决这个问题.

Login.ashx的伪代码:

string login = GetParameter("login", context);
string passhash = GetParameter("pass", context);
string challenge = "" + Context.Session["CHALLENGE"];
if (!string.IsNullOrEmpty(challenge))
{
  // this is the 'response' part
  string challengeResponse = Crypto.GetChallengeResponse(Challenge, UserFromDB.PassHash);
  if (challengeResponse == passhash)
  {
    // Great success, challenge matches the response
    Log.I("Success");
    return "SUCCESS";
  }
  else
  {
    Log.W("Failed to respond");
    return "FAILED TO RESPOND";
  }
}
else
{
  // if passed login or session-stored challenge are empty - issue a new challenge
  challenge = "Challenge: "+ Crypto.GetRandomToken();
  Context.Session["CHALLENGE"]  = challenge;
  Log.I("Sent Challenge"); // this is what's in the log below
  return challenge;
}
Run Code Online (Sandbox Code Playgroud)

这是日志,每次调用都会出现Session,Session.Keys.Count保持为0,即使应该设置Session ["CHALLENGE"]:

// This is the challenge request:
[] **Session started**: sr4m4o11tckwc21kjryxp22i Keys: 0  AppDomain: /LM/W3SVC/1/ROOT-4-130081332618313933 #44 
[] Processing: <sv> **MYWEBSITE/ajax/Login.ashx** SID=sr4m4o11tckwc21kjryxp22i  
[] Sent Challenge @Login.ashx.cs-80 

// this is the response, note that there's another Session started with the same id
// and the session didn't keep the value ["CHALLENGE"], there are no session-end events either
[] **Session started**: sr4m4o11tckwc21kjryxp22i Keys: 0  AppDomain: /LM/W3SVC/1/ROOT-4-130081332625333945 #93  
[] Processing: <sv> **MYWEBSITE/ajax/Login.ashx?login=MYLOGIN&pass=RuhQr1vjKg_CDFw3JoSYTsiW0V0L9K6k6==**
[] Sent Challenge @Login.ashx.cs-80 >Session: sr4m4o11tckwc21kjryxp22i 
Run Code Online (Sandbox Code Playgroud)

网络配置,消毒

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="IncludeStackTraceInErrors" value="false" />
  </appSettings>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
    <add name="MYConnection" connectionString="metadata=res://*…. and a bunch of other stuff that works" providerName="System.Data.EntityClient" />
  </connectionStrings> 
   <system.web>
    <compilation targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    <pages controlRenderingCompatibilityVersion="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Pau*_*hra 0

空闲超时的默认值是多少?如果应用程序池超时,您的会话就会消失

请参阅应用程序池(高级设置)-> 空闲超时

我认为默认为五分钟。

有关设置空闲超时的建议,请参阅此链接

如果您在不需要时作为网络花园运行,您也可能会遇到问题;查看最大工作进程数,尝试将其设置为 1 并重新测试