积极答复者
关于ASP.NET Forms 身份验证永久保存用户登录状态

问题
-
http://files.cnblogs.com/ret00100/WebSite1.rar
上面简单的例子。
但为什么FormsAuthentication.RedirectFromLoginPage(UserName, true);//这怎么不是永久保存??用了FF看了设置为true的话就30分钟后过期。如何有操作的话例如29分钟刷新页面了,到了到期时间它会自动的再延长30分钟。但这个不是很好啊。
msdn好像是说RedirectFromLoginPage第二的参数设置为true的话是永久?
是不是我设置有问题呢??
答案
全部回复
-
- createPersistentCookie
若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false。
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~ -
- createPersistentCookie
- 类型:System..::.Boolean
指定是否必须发出持久 Cookie(基于会话或比会话更持久的 Cookie)。
那如果要创建永久的登录状态呢?又该怎么做
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
Response.Redirect("admin/Default.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
string UserName = TextBox1.Text;FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.MaxValue, true, "");
string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, true));
// FormsAuthentication.RedirectFromLoginPage(UserName, false);//这怎么不是永久保存??
}
这样的话FF看。显示cookies 是会话结束时。DateTime.MaxValue。。了 。。这么还是这样想不明白。。有高手知道吗 -
还有这里我不用RedirectFromLoginPage了。。注释了。用另外的方法。把时间改为最大了还是不能永久保存哦Why- createPersistentCookie
- 类型:System..::.Boolean
指定是否必须发出持久 Cookie(基于会话或比会话更持久的 Cookie)。
那如果要创建永久的登录状态呢?又该怎么做
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
Response.Redirect("admin/Default.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
string UserName = TextBox1.Text;FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.MaxValue, true, "");
string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, true));
// FormsAuthentication.RedirectFromLoginPage(UserName, false);//这怎么不是永久保存??
}
这样的话FF看。显示cookies 是会话结束时。DateTime.MaxValue。。了 。。这么还是这样想不明白。。有高手知道吗 -
我没有设置30分钟!!!!
但 <authentication mode="Forms">
这只是之前的测试设置30分钟的话。FormsAuthentication.RedirectFromLoginPage(UserName, true);为true的话!:这样时间30分钟没错
这里不设置timeout=30的话就会是会话结束时结束哦?
</authentication>
为什么?还有这么设置永久保存
否则不设置30分钟FormsAuthentication.RedirectFromLoginPage(UserName, true);为true的话:会话结束时结束
我上面给我代码没有设置30的!!
不设置的话 默认是30 ??
默认不是30!
默认是关闭浏览器就结束。不信你试试!
h呵呵 我只是从asp.net2.0书上看说明
很遗憾 我照的你的方法 去测试了 默认不是关闭浏览器结束 无论ff ie还是显示 设置false是关闭滤清器结束 -
但是你不觉得直接保存一个99年或者更久的,不好吗。我只是觉得奇怪。
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.MaxValue, true, "");
string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, true));
这样DateTime.MaxValue也不能 -
额.。试过了。。好像也不行啊
而且
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.MaxValue, true, "");
MaxValue了。。
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="admin/Default.aspx" name="cs">
</forms>
</authentication>
protected void Button1_Click(object sender, EventArgs e)
{
string UserName = TextBox1.Text;FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddDays(10), true, "");
string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName, true));
}
过期时间:当会话结束时。。。 why? -
Cookie的最长时间为1年的~~~
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~ -
创建50年Forms验证的全过程:1,web.config
<?xml version="1.0"?> <configuration> <system.web> <authentication mode="Forms"> <forms name="LoginForm" loginUrl="Login.aspx" path="/" timeout="26280000" slidingExpiration="true"> </forms> </authentication> <authorization> <deny users="?"/> <allow users="*"/> </authorization> </system.web> <system.codedom></system.codedom> <system.webServer></system.webServer> </configuration>
Login.aspx<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { String username = UserName.Text; if (username.EndsWith("mxh", StringComparison.InvariantCultureIgnoreCase) && Password.Text.EndsWith("mxh", StringComparison.InvariantCultureIgnoreCase)) { FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2,//票证的版本号 username,//与身分验证票关联的用户名 DateTime.Now, //票证发出时的本地日期和时间 DateTime.Now.AddYears(50),//票证过期的本地日期和时间 true,// 如果票证存储在持久性cookie中(跨浏览器会话保存)则为 true 否则为false 如果票证储存在URL中,将忽略此值 "admin|reader|editor",//储存在票证中持定的用户信息 FormsAuthentication.FormsCookiePath //票证储存在cookie中的路径 ); //如果 forms 元素的 protection 属性设置为 All 或 Encryption,则窗体身份验证使用 Encrypt 方法对窗体身份验证票进行加密和签名。 /* string encTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); cookie.Path = FormsAuthentication.FormsCookiePath; cookie.Domain = FormsAuthentication.CookieDomain; cookie.Expires = DateTime.Now.AddYears(50); */ FormsAuthentication.RedirectFromLoginPage(username, true); //Response.Cookies.Add(cookie); //Response.Redirect("Default6.aspx"); } else { Response.Write("密码不正确"); } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Password.Attributes.Add("value", "mxh"); } } </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 id="Head1" runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> 用户名:<asp:TextBox ID="UserName" runat="server" Text="mxh" />mxh <br /> 密码:<asp:TextBox ID="Password" TextMode="password" runat="server" Text="mxh" />mxh<br /> <asp:Button ID="Button1" Text="登录" runat="server" OnClick="Button1_Click" /> </div> </form> </body> </html>
测试显示页面页面Default6.aspx<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { Response.Write(Page.User.Identity.IsAuthenticated.ToString() + " = " + Page.User.Identity.Name); Response.Write(DateTime.Now.ToString()); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> </form> </body> </html>
将以上代码放在你的站点里,注意:如果你测试没有效果,请原封不动地拷贝测试以本地时间访问Default6.aspx,点击登录,关闭所有浏览器窗口。将计算机日期时间调整到2049年,再打开浏览器浏览Default6.aspx
【孟子E章】 -
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
strUserName,
DateTime.Now,
DateTime.Now.AddYears(30),
true,
"",
FormsAuthentication.FormsCookiePath);
string encTicket = FormsAuthentication.Encrypt(ticket);
// Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
HttpCookie c = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
if (Ckbset.Checked)
c.Expires = DateTime.Now.AddYears(99);
Response.Cookies.Add(c);
额像这样 protection 属性设置为 All 。。。吗?