如何在asp.net formsAuthentication中超时用户
我想知道如果一个用户在10分钟后没有做任何请求,我该如何设置一个超时,比如说会话被终止并且他们注销了 我的网络配置中有这个如何在asp.net formsAuthentication中超时用户,.net,asp.net,asp.net-mvc,web-config,.net,Asp.net,Asp.net Mvc,Web Config,我想知道如果一个用户在10分钟后没有做任何请求,我该如何设置一个超时,比如说会话被终止并且他们注销了 我的网络配置中有这个 <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" protection="All" timeout="20160" path="/"
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn"
protection="All"
timeout="20160"
path="/"
requireSSL="false"
slidingExpiration="false"
defaultUrl="default.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false" />
</authentication>
当我在我的webconfig中设置会话状态时,我的url中有这个
(S(gkvkze55zfzzee45wj34byee))
我不想在我的代码中出现这一令人讨厌的行。我假设您的超时是由会话超时引起的,而不是身份验证超时引起的 检查web.config中的会话状态节点。
<sessionState mode="InProc"
cookieless="true"
timeout="60"/>
您的表单身份验证票证不能同时有滑动和绝对过期 有关了解ASP.NET中表单身份验证的概述和教程链接,请参见我的答案 更新: 如果出现以下情况,如何为用户设置超时 他们不做任何要求后说 10分钟后,会话被终止,并且 它们已注销 注销=形成身份验证,并与会话(状态)正交(例如存储数据的位置)
简单的答案是不要在会话中存储数据。查看与所需内容类似的内容。另一个答案,只是为了说明您可能希望如何使用web.config中的值创建cookie,而不是在代码中硬编码它们 首先,考虑是否需要所有额外的选择。最简单的方法是在web.config中设置所有内容
FormsAuthentication.RedirectFromLoginPage("Bob", isPersistent)
但是,如果您需要将UserData添加到票证中,则必须创建自己的。注意我们如何使用web.config中的值而不是硬编码值
/// <summary>
/// Create a New Forms Authentication Ticket when User Impersonation is active, using the current ticket as a basis for the new ticket.
/// </summary>
private static void NewTicket(MyUser currentUser,
string userData,
bool createPersistentCookie)
{
System.Web.Configuration.AuthenticationSection authSection =
(System.Web.Configuration.AuthenticationSection)
ConfigurationManager.GetSection("system.web/authentication");
System.Web.Configuration.FormsAuthenticationConfiguration
formsAuthenticationSection = authSection.Forms;
DateTime now = DateTime.Now;
// see http://msdn.microsoft.com/en-us/library/kybcs83h.aspx
// Create a new ticket used for authentication
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
2, // Ticket version
currentUser.UserName, // Username to be associated with this ticket
now, // Date/time issued
now.Add(formsAuthenticationSection.Timeout),// Date/time to expire
createPersistentCookie,
userData,
FormsAuthentication.FormsCookiePath);
// Hash the cookie for transport over the wire
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie (specified in web.config)
hash); // Hashed ticket
// Add the cookie to the list for outbound response
HttpContext.Current.Response.Cookies.Add(cookie);
}
//
///当用户模拟处于活动状态时,使用当前票据作为新票据的基础,创建新的表单身份验证票据。
///
私有静态void NewTicket(MyUser currentUser,
字符串userData,
bool createPersistentCookie)
{
System.Web.Configuration.AuthenticationSection AuthenticationSection=
(System.Web.Configuration.AuthenticationSection)
ConfigurationManager.GetSection(“system.web/authentication”);
System.Web.Configuration.FormsAuthenticationConfiguration
formsAuthenticationSection=authSection.Forms;
DateTime now=DateTime.now;
//看http://msdn.microsoft.com/en-us/library/kybcs83h.aspx
//创建用于身份验证的新票证
FormsAuthenticationTicket=新FormsAuthenticationTicket(
2、//票证版本
currentUser.UserName,//要与此票证关联的用户名
现在,//发布日期/时间
Add(formsAuthenticationSection.Timeout),//到期日期/时间
createPersistentCookie,
用户数据,
FormsAuthentication.FormScookePath);
//散列cookie以便通过网络传输
字符串哈希=FormsAuthentication.Encrypt(票据);
HttpCookie cookie=新的HttpCookie(
FormsAuthentication.FormScookeName,//身份验证cookie的名称(在web.config中指定)
散列);//散列票据
//将cookie添加到出站响应的列表中
HttpContext.Current.Response.Cookies.Add(cookie);
}
当用户已经登录时,可以使用相同的技术重新创建票据。例如,如果您需要更改
Ticket.UserData
。发布新票证时,您将增加版本号。我的网络配置中没有会话状态。默认为什么?您的问题中提到了“会话”。这就是我所指的属性。我不知道会话状态有一个单独的属性。我想这就是formsauth所做的。在创建formsauth票证时,请检查“DateTime.Now.AddMinutes(30),//expiration”。FormsAuthenticationTicket票证=新的FormsAuthenticationTicket(1,//版本txtEmail.Text,//名称DateTime.Now,//issueDate DateTime.Now.AddMinutes(30),//到期假,//isPersistent角色,//用户数据FormsAuthentication.FormScookePath//cookiePath);默认会话超时为10分钟。对不起,我不懂(或者我想我不懂)。我没有设定滑动有效期。我只是有一个绝对有效期,要么是会话结束,要么是从登录日期起2周。我已经用另一个链接更新了我的答案,该链接指向另一个关于ASP.NET和会话/身份验证的答案。请阅读参考信息,特别是链接和教程视频。我现在把它放在幻灯片上,但它仍然不会让我登录2周。我不知道为什么。@chobo2,你不能滑动并说“但最多只能两周”。另外,您是否正在关闭浏览器?如果您没有将身份验证cookie设置为持久,cookie本身将不会保存到磁盘,因此重新打开浏览器时您将不会登录。另请参见msdn:关于URL中的sessionID,您必须在web.config中将cookieless=“true”更改为“false”。这样,您就不会在URL中有SessionID。
/// <summary>
/// Create a New Forms Authentication Ticket when User Impersonation is active, using the current ticket as a basis for the new ticket.
/// </summary>
private static void NewTicket(MyUser currentUser,
string userData,
bool createPersistentCookie)
{
System.Web.Configuration.AuthenticationSection authSection =
(System.Web.Configuration.AuthenticationSection)
ConfigurationManager.GetSection("system.web/authentication");
System.Web.Configuration.FormsAuthenticationConfiguration
formsAuthenticationSection = authSection.Forms;
DateTime now = DateTime.Now;
// see http://msdn.microsoft.com/en-us/library/kybcs83h.aspx
// Create a new ticket used for authentication
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
2, // Ticket version
currentUser.UserName, // Username to be associated with this ticket
now, // Date/time issued
now.Add(formsAuthenticationSection.Timeout),// Date/time to expire
createPersistentCookie,
userData,
FormsAuthentication.FormsCookiePath);
// Hash the cookie for transport over the wire
string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie (specified in web.config)
hash); // Hashed ticket
// Add the cookie to the list for outbound response
HttpContext.Current.Response.Cookies.Add(cookie);
}