会话超时已处理,但在 ASP.NET 中仍然无法工作?
我在 web.config 中将超时设置为 30 分钟,如下所示
<forms name=".FormsAuth" loginUrl="/Login.aspx" timeout="30" protection="All"
slidingExpiration="true" >
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424"
stateNetworkTimeout="300" sqlCommandTimeout="300" sqlConnectionString="data
source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="30" />
在母版页中。我将 SeTtime 设置为 30 分钟,这样它就可以重定向登录页面。
Dim sessionExpiredUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) & "/labor"
Dim script As New StringBuilder()
script.Append("function expireSession(){ " & vbLf)
script.Append(String.Format(" window.location = '{0}';" & vbLf, sessionExpiredUrl))
script.Append("} " & vbLf)
script.Append(String.Format("setTimeout('expireSession()', {0}); " & vbLf, Me.Session.Timeout * 60000))
' Convert minutes to milliseconds
Me.Page.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "expirescript", script.ToString(), True)
它工作正常。 30 分钟后,它会重定向到登录页面。但是如果我点击会话对象为“25 分钟”的页面。它抛出异常[NullReferenceException:未将对象引用设置到对象的实例。]
。
我不明白为什么会话对象在 30 分钟前变成“空”以及如何处理这个问题?
编辑: 这里发生错误。
mUser = CType(Session("user"), User)
Dim processTime As String = mUser.GetLastProcessTime(lastProcessTime) // Error happening here
I set timeout for 30 mins in web.config like below
<forms name=".FormsAuth" loginUrl="/Login.aspx" timeout="30" protection="All"
slidingExpiration="true" >
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424"
stateNetworkTimeout="300" sqlCommandTimeout="300" sqlConnectionString="data
source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="30" />
In master page page . I set SeTtime of 30 mins so it can redirect login page.
Dim sessionExpiredUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) & "/labor"
Dim script As New StringBuilder()
script.Append("function expireSession(){ " & vbLf)
script.Append(String.Format(" window.location = '{0}';" & vbLf, sessionExpiredUrl))
script.Append("} " & vbLf)
script.Append(String.Format("setTimeout('expireSession()', {0}); " & vbLf, Me.Session.Timeout * 60000))
' Convert minutes to milliseconds
Me.Page.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "expirescript", script.ToString(), True)
It is working fine . after 30 mins it is redirect to login page. But If I click pages that have session object at "25 MINS". It is throwing exception [NullReferenceException: Object reference not set to an instance of an object.]
.
I don't understand why session objects becoming "Null" before 30 mins and how to handle this?
Edit:
Error happening here.
mUser = CType(Session("user"), User)
Dim processTime As String = mUser.GetLastProcessTime(lastProcessTime) // Error happening here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是 InProc,您的应用程序池可能会由于不活动而重置或关闭,从而使会话无效。看看这篇文章 看看是不是你的问题。
Since you are using InProc, possibly your app pool is reseting or shutting down due to inactivity, and that invalidates the session. Take a look at this article to see if it is your problem.
这是因为您设置了 SlidingExpiration = true,并且如果发出请求并且超过了一半的超时时间,则滑动过期会重置有效身份验证 cookie 的过期时间。
http://msdn.microsoft.com/en -us/library/system.web.security.formsauthentication.slidingexpiration.aspx
It's because you set SlidingExpiration = true and Sliding expiration resets the expiration time for a valid authentication cookie if a request is made and
more than half of the timeout
interval has elapsed.http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.slidingexpiration.aspx