IIS7会话失去了它的价值

时间:2013-03-19 03:08:40

标签: c# asp.net asp.net-ajax session-variables

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

我在Global.asax.cs中放置了Session_StartSession_End(和其他一些)方法,并在那里记录了一些日志,我看到一个新的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;
}

这是日志,每次调用都会出现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 

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>

3 个答案:

答案 0 :(得分:0)

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

请参阅应用程序池(高级设置) - &gt;闲置超时

我认为默认为五分钟。

请参阅this link for advice on setting the idle timeout

如果您在不需要的情况下作为webgarden运行,您也可能会遇到问题;查看Maximum Worker Processes,尝试将其设置为1并重新测试

答案 1 :(得分:0)

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

答案 2 :(得分:0)

将IRequiresSessionState添加到您的处理程序实现

public class handler_name:IHttpHandler,IRequiresSessionState