简体   繁体   English

ASP.NET 会话状态在 Web.config 和 Global.asax 中不起作用

[英]ASP.NET Session State not working in Web.config as well as Global.asax

I have Admin Page wherein I use to do some update work for my site.我有一个管理页面,我用来为我的网站做一些更新工作。 The problem is the session will expire within a minute or 30 seconds and will logout the user.问题是会话将在一分钟或 30 秒内过期,并将注销用户。 I have set the Session in Web.Config of the root folder as well as in the Web.Config inside the Admin folder but still the session gets expired soon.我已经在根文件夹的 Web.Config 以及 Admin 文件夹内的 Web.Config 中设置了会话,但会话仍然很快过期。 I have set it to 60 minutes but it only lasts for 30 seconds or within a minute.我已将其设置为 60 分钟,但它仅持续 30 秒或一分钟内。 Here is my web.config content of root folder这是我的根文件夹的 web.config 内容

<sessionState timeout="60" mode="InProc" 
      cookieless="false"></sessionState>


    <customErrors mode="Off">
    </customErrors>
    <trace enabled="true" />
    <authentication mode="Forms">
               <forms
    protection="All"
    timeout="120"
    domain="www.marpallichande.in"
    slidingExpiration="true"
    name="auth_cookie" />  

    </authentication>

and this is my setting of web.cofing file inside the Admin folder这是我在 Admin 文件夹中设置的 web.cofin 文件

<sessionState timeout="60" mode="InProc"
      cookieless="false"></sessionState>

and this is my setting in Global.asax file under Session_Start method这是我在 Session_Start 方法下的 Global.asax 文件中的设置

Session.Timeout=60;

I am not getting how the session is getting expired so soon or is there any other reason for been getting logged out other than session.我不知道会话是如何这么快过期的,或者除了会话之外还有其他任何原因导致注销。

sessionState timeout value is in minutes. sessionState超时值以分钟为单位。 I would start by removing Session.TimeOut (and any other timeout values except sessionState timeout, leave it as it is and give it a try. Also, not sure why you have two config files? Do they have same settings? 我将首先删除Session.TimeOut(以及除sessionState超时之外的任何其他超时值,保持原样并试一试。另外,不确定为什么你有两个配置文件?他们有相同的设置吗?

I have a similar setup but just one config file with 我有一个类似的设置,但只有一个配置文件

<sessionState mode="InProc" cookieless="false" timeout="10" /> 

setting it to 10 minutes. 设置为10分钟。

write <sessionState mode="InProc" cookieless="false" timeout="10" /> in the global web.config file of your application. 在应用程序的全局web.config文件中写入<sessionState mode="InProc" cookieless="false" timeout="10" />

define Session_OnEnd event by adding a subroutine named Session_OnEnd to the Global.asax file. 通过将名为Session_OnEnd的子例程添加到Global.asax文件来定义Session_OnEnd事件。 The Session_OnEnd subroutine is run when the method has been called or when the session has expired. Session_OnEnd子例程在调用方法或会话已过期时运行。 A session expires when the number of minutes specified by the Timeout property passes without a request being made for the session. 会话在Timeout属性指定的分钟数通过而没有为会话发出请求时到期。

The Session_OnEnd event is supported only when the session state Mode property is set to InProc. 仅当会话状态Mode属性设置为InProc时,才支持Session_OnEnd事件。

Session_onEnd event can be defined in global.asax as: Session_onEnd事件可以在global.asax中定义为:

public void Session_OnEnd()
{
  // do your desired task when the session expires
}

Check the root web.config for a session state setting such as: 检查根web.config以获取会话状态设置,例如:
//Timeout is in minutes //超时是几分钟
//Note for using the global.asx about InProc: SessionStateModule.End Event //关于使用关于InProc的global.asx的注意事项: SessionStateModule.End事件

The Session_OnEnd event is only supported when the session-state HttpSessionState.Mode property value is InProc, which is the default. 仅当会话状态HttpSessionState.Mode属性值为InProc(默认值)时,才支持Session_OnEnd事件。 If the session-state Mode is set to StateServer or SQLServer, then the Session_OnEnd event in the Global.asax file is ignored. 如果会话状态模式设置为StateServer或SQLServer,则忽略Global.asax文件中的Session_OnEnd事件。 If the session state Mode property value is Custom, then support for the Session_OnEnd event is determined by the custom session-state store provider. 如果会话状态Mode属性值为Custom,则自定义会话状态存储提供程序将确定对Session_OnEnd事件的支持。

<system.web>
<sessionState mode="InProc" cookieless="false" timeout="30" />
</system.web>

You should only have this defined in 1 location. 您应该只在1个位置定义它。

if page logout after some seconds automatically that is because of session expire 如果页面在几秒钟后自动 退出 ,那是因为会话过期

 <sessionSate mode="StateServer" cookieless="false" timeout="940"/> 

write the code in web.config 在web.config中编写代码

I find the solution from enter link description here 我在这里输入链接描述找到解决方案

There is a setting in IIS as well that may need to be updated, the "idle timeout" setting. IIS 中还有一个可能需要更新的设置,即“空闲超时”设置。 The difference is explained here . 此处解释差异。

By default Idle Timeout is set to 20 minutes, so you will want to set that to 180. This can be done by going into IIS > Application Pools > Right Click Your App Pool > Advanced Settings:默认空闲超时设置为 20 分钟,因此您需要将其设置为 180 分钟。这可以通过进入 IIS > 应用程序池 > 右键单击​​您的应用程序池 > 高级设置来完成:

在此处输入图片说明

I had the same issue in my ASP.NET MVC web project.我在我的ASP.NET MVC Web 项目中遇到了同样的问题。 Using the following method to set the session duration in the Global.asax.cs file works:使用以下方法在Global.asax.cs文件中设置会话持续时间有效:

public class ApplicationName : System.Web.HttpApplication
{
    protected void Session_Start(object sender, EventArgs e)
    {
        /* Sets the session duration to 60 minutes. */
        Session.Timeout = 60;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM