表单身份验证超时和会话超时的差异
使用此 web.config 元素设置会话状态超时 使用
<sessionState mode="InProc" cookieless="false" timeout="120" />
此 web.config 元素配置表单身份验证
<system.web>
<authentication mode="Forms">
<forms loginUrl="Login.aspx"
protection="All"
timeout="30"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="default.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />
</authentication>
</system.web>
每个元素中指定的超时之间有什么区别?如果两者不同,它会如何工作?
The session state timeout is set using this web.config element
<sessionState mode="InProc" cookieless="false" timeout="120" />
The forms auth is configured using this web.config element
<system.web>
<authentication mode="Forms">
<forms loginUrl="Login.aspx"
protection="All"
timeout="30"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="default.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />
</authentication>
</system.web>
What is the difference between the timeouts specified in each of these elements? If both are different, how would it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每当新用户访问网站时,无论他们是否匿名,都会启动会话。身份验证与会话关系不大。
身份验证超时是身份验证 cookie 在用户浏览器上有效的时间量。一旦 cookie 过期,他们必须重新进行身份验证才能访问网站上受保护的资源。
因此,如果会话在身份验证 cookie 之前超时 - 它们仍然经过身份验证,但所有会话变量都会消失,并且如果您不遵守检查空值和丢失会话所带来的其他条件的规定,可能会导致您的网站出现错误。
如果身份验证在会话之前超时,则其所有会话变量仍将存在,但在重新登录之前将无法访问受保护的资源。
A session starts every time a new user hits the website, regardless of whether or not they are anonymous. Authentication has very little to do with Session.
Authentication timeout is the amount of time that the authentication cookie is good for on the user's browser. Once the cookie expires, they must re-authenticate to access protected resources on the site.
So, if Session times out before the Authentication cookie - they are still authenticated, but all their session variables disappear, and may cause errors in your website if you are not disciplined in checking for nulls and other conditions brought about by missing session.
If Authentication times out before the session, then all their session variables will still exist, but they won't be able to access protected resources until they log back in again.
正如预期的那样。
例如,如果您的会话在 20 分钟后超时,您的会话变量将丢失。
但用户可以访问受身份验证保护的页面。
如果身份验证超时,用户将无法访问其保护的页面,并且会话的状态无关紧要。
as expected.
e.g. if your session times out after 20 minutes, your session-variables will be lost.
but the user could access the pages which are protected by the authentication.
if the authentication times out, the user could not access the page which it protects, and the state of the session is irrelevant.
会话超时值必须小于 FormsAuthentication 超时时间。因为会话可能由于某种原因而被删除,并且该场景将不起作用。
如果会话超时,请检查 FormsAuthentication Ticket。如果票证有效且未超时,则在登录页面重新生成会话(在 web.config 文件中定义为 FormsAuthentication 设置的 LoginUrl 参数)。
如果 FormsAuthentication 超时,ASP.NET FormsAuthentication 将自动将用户重定向到登录页面,并且用户必须再次登录。
不要忘记,当票证超时时,FormsAuthentication 会自动将用户重定向到登录页面。
实际上我更喜欢这种结构:
在 BasePage.cs 中的 Page_Init 处检查会话。如果会话已过期;将用户重定向到登录页面。
if (Session["UserId"] == null) Response.Redirect("Login.aspx", true);
在 Login.aspx 中的 Page_Load 处,正确检查会话和 FormsAuthentication 票证时间。
在 Login.aspx 中使用 FormsAuthentication 创建 cookie。
if (用户名 == "testuser" && 密码 == "testpassword")
{
//用户数据将被写入cookie,存储用户数据,您可以检查用户是否有效(例如Username或UserId)。
System.Web.Security.FormsAuthentication.SetAuthCookie("testUserData", keepMeSignedInCheckBox.Checked);
Response.Redirect("HomePage.aspx");
}
Session Timeout value must be smaller than FormsAuthentication timeout time. Because Session can be removed because of a reason and the scenario won't work.
If Session times out, check FormsAuthentication Ticket. If ticket is valid and not time out, then re-generate session at your Login page (defined in your web.config file as LoginUrl parameter of FormsAuthentication settings).
If FormsAuthentication times out, ASP.NET FormsAuthentication redirects user automatically to Login Page and user has to login again.
Don't forget that FormsAuthentication automatically redirects user to login page when ticket time outs.
Actually I prefer this structure:
at Page_Init in BasePage.cs check Session. If Session Expired; redirect user to Login page.
if (Session["UserId"] == null) Response.Redirect("Login.aspx", true);
At Page_Load in Login.aspx check Session and FormsAuthentication Ticket times properly.
at Login.aspx use FormsAuthentication to create cookies.
if (username == "testuser" && password == "testpassword")
{
//User data will be written to a cookie, store a user data that you can check user is valid or not (for example Username or UserId).
System.Web.Security.FormsAuthentication.SetAuthCookie("testUserData", keepMeSignedInCheckBox.Checked);
Response.Redirect("HomePage.aspx");
}