ASP.NET 中身份验证会话超时时控制值丢失
我有一个使用表单身份验证的 ASP.NET 网站。超时时间是二十分钟。
我注意到,如果用户完成了一半的表单,然后超时,他们将被重定向到登录页面,填写内容,然后重定向回表单,但带有空控件。
我之前曾假设发生这种情况时 ASP.NET 会使用一些欺骗手段来重新填充表单控件。
我可以做一些改变来确保它确实如此吗?
I have a ASP.NET website which uses forms authentication. The timeout is twenty minutes.
I have noticed that if a user half completes a form, and is then timed out, they are redirected to the login page, fill it in, and are redirected back to the form but with EMPTY controls.
I had prevously assumed that ASP.NET would use some skulduggery to repopulate the form controls when this happens.
Are there changes I can make to make sure it does?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为解决您问题的最佳方法是设置 将 web.config 中表单身份验证元素上的
slidingExpiration
属性设置为true
。这样,只有当用户填写表单的时间超过 20 分钟时,才会丢失其数据。出现此问题的原因是,asp.net 通过名为“视图状态”的概念“记住”在表单中输入的值,该概念基本上只是一个隐藏的表单字段。当服务器重定向到登录页面时,所有表单字段都会丢失,因为重定向不能包含 POST 数据。
如果您希望用户能够花 20 分钟以上的时间来填写表单,您可以考虑在包含表单的页面上添加一些 javascript,每 x 分钟对服务器进行一次 ajax 调用。通过滑动过期,这将在每次触发 ajax 调用时重置会话过期时间,因此只要用户正在查看包含表单的页面,它就永远不会注销您的用户。
I think the best solution to your problem is to set the
slidingExpiration
attribute totrue
on the forms authentication element in web.config. This way the user only loses his/her data when they take in excess of 20 minutes to fill in the form.The problem occurs because asp.net "remembers" values that have been entered in the form by means of a concept called viewstate, which is basically just a hidden form field. When the server does a redirect to the login page, all form fields are lost because a redirect cannot contain POST data.
If you want your users to be able to take longer than 20 minutes to fill in the form, you can consider having some javascript on the page containing the form, which makes an ajax call to the server every x minutes. With sliding expiration, this will reset the session expiration time everytime the ajax call is fired, and thus it will never log your user out as long as they are viewing the page containing the form.
通过将 AJAX 样式的
WebMethod
附加到 onChange 事件,您可以在填充每个控件的内容后立即将其动态保存到 Session。使用此方法,只要用户每 20 分钟至少认真填写一个控件,会话就不会超时。
You could dynamically save the contents of each control to Session as soon as it's filled in by attaching an AJAX-style
WebMethod
to the onChange event.Using this method, the Session wouldn't time out so long as the user was diligently filling in at least one control every 20 minutes.