Forms authentication 使用forumsauthentication创建持久化cookie
我在登录期间执行以下操作,但登录似乎根本不会持久:Forms authentication 使用forumsauthentication创建持久化cookie,forms-authentication,Forms Authentication,我在登录期间执行以下操作,但登录似乎根本不会持久: FormsAuthentication.SetAuthCookie(userId.ToString(), true); 您遇到了一个错误,MS称之为未记录的安全功能 为了设置持久cookie,您需要自己创建并显式设置过期时间。唯一的诀窍是获取FormsAuthenticationTimeout值,这是微软自1.0以来从未公开过的。我已经提供了获取此值的方法 下面是一个工作示例 Login.aspx <%@ Page Language="
FormsAuthentication.SetAuthCookie(userId.ToString(), true);
您遇到了一个错误,MS称之为未记录的安全功能 为了设置持久cookie,您需要自己创建并显式设置过期时间。唯一的诀窍是获取FormsAuthenticationTimeout值,这是微软自1.0以来从未公开过的。我已经提供了获取此值的方法 下面是一个工作示例 Login.aspx
<%@ Page Language="C#" %>
<script runat="server">
protected void Login1_LoggedIn(object sender, EventArgs e)
{
var login = (Login)sender ;
if (login.RememberMeSet)
{
// hack to get forms timeout - it is not publicly surfaced anywhere.
var tmpTicket = FormsAuthentication.GetAuthCookie("foo", true);
var timeout = tmpTicket.Expires;
// create a new ticket
FormsAuthenticationTicket ticket =
new FormsAuthenticationTicket(2, login.UserName, DateTime.Now, timeout, true, "", FormsAuthentication.FormsCookiePath);
string ticketEncrypted = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted)
{
HttpOnly = true,
Path = FormsAuthentication.FormsCookiePath,
Secure = FormsAuthentication.RequireSSL,
Expires = ticket.Expiration
};
Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
Response.Cookies.Add(cookie);
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" OnLoggedIn="Login1_LoggedIn">
</asp:Login>
</div>
</form>
</body>
</html>
受保护的void Login1_LoggedIn(对象发送方,事件参数e)
{
var login=(login)发送方;
if(login.rememberset)
{
//黑客获取表单超时-它没有公开在任何地方出现。
var tmpTicket=FormsAuthentication.GetAuthCookie(“foo”,true);
var timeout=tmpTicket.Expires;
//创建新票证
FormsAuthenticationTicket=
新的FormsAuthenticationTicket(2,login.UserName,DateTime.Now,timeout,true,“,FormsAuthentication.FormScookePath);
string ticketEncrypted=FormsAuthentication.Encrypt(票据);
HttpCookie cookie=新的HttpCookie(FormsAuthentication.FormScookeName,ticketEncrypted)
{
HttpOnly=true,
路径=FormsAuthentication.FormsScookePath,
Secure=FormsAuthentication.RequireSSL,
过期=票证过期
};
Response.Cookies.Remove(FormsAuthentication.formscookeName);
Response.Cookies.Add(cookie);
}
}
在web.config中,表单身份验证超时设置为什么?用户ID是否也保存在会话或任何其他持久存储机制中?是超时设置为30。未使用其他持久性存储。是否可以从web.config粘贴分区?另外,别忘了再次检查你的浏览器是否没有设置为在关闭时清除cookies。那么,你能定义一下你所说的“看起来根本没有持久化”是什么意思吗?您的用户是否可以根本不登录应用程序,或者在关闭浏览器后是否已注销?