IIS7会话值丢失

本文关键字:会话 IIS7 | 更新日期: 2023-09-27 18:04:31

我已经实现了一个作为Ajax处理程序的质询-响应方案。由于某种原因,它在正常工作了几个月后就停止工作了。调查发现,Context.Session[KEY]在挑战和响应调用之间失去了其价值。

我把Session_StartSession_End(和其他一些)方法在Global.asax.cs有一些日志记录,我看到一个新的Session_Start事件被触发相同的会话ID,没有Session_End事件

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

更新:我尝试切换到SQLServer会话,但没有改变行为。在极少数情况下,会话会像预期的那样工作,不知道为什么。我尝试了所有我能找到的"会话丢失变量"故障排除指南,但没有效果

UPDATE 2:我将问题缩小到缺少会话cookie,但修改我的。浏览器的配置在多次尝试后没有解决这个问题。当我从浏览器调用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;
}

这是日志,Session started出现在每次调用中,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 

web config, sanitized

<?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>

IIS7会话值丢失

空闲超时的默认值是多少?如果应用程序池超时,会话将bye bye

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

我想默认是五分钟。

请参阅此链接获取关于设置空闲超时的建议

如果你在不需要的时候运行webgarden,你也会遇到你的问题;查看最大工作进程,尝试将其设置为1并重新测试

我可以看到你使用处理程序的目的,这将始终返回null。你需要实现IReadOnlySessionState。查看http://www.hanselman.com/blog/GettingSessionStateInHttpHandlersASHXFiles.aspx

将IRequiresSessionState添加到处理程序实现中

交货

公共类handler_name:IHttpHandler, IRequiresSessionState