问题是:为什么我每次关掉浏览器后在访问index.aspx时还要重新登陆(每次打开都是过期的,是不是我的思路不对)
index.aspx.cs中该如何调用,判断?帮帮我各位!先谢谢!
login.aspx.cs页面:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, Session["username"].ToString(), DateTime.Now,
DateTime.Now.AddMinutes(30),
false, Session["username"].ToString()); string cookieStr = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieStr);
cookie.HttpOnly = true;
cookie.Path = FormsAuthentication.FormsCookiePath;
//cookie.Expires = ticket.Expiration;
Context.Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage(Session["username"].ToString(), false);
web.Config:
<authentication mode="Forms">
<forms name="ASPXFORMSAUTH" loginUrl="login.aspx" timeout="20" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<sessionState mode="InProc" timeout="20" cookieless="UseCookies" />//=true
index.aspx.cs:
if (FormsAuthentication.FormsCookieName.ToString() == null)
{
Response.Redirect("login.aspx");
}
else
{
FormsAuthenticationTicket Ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
Response.Write(Ticket.Expiration.ToString());//这里输出的时间是在当前时间上加20分钟!
Session["username"] = Ticket.Name;
}
index.aspx.cs中该如何调用,判断?帮帮我各位!先谢谢!
login.aspx.cs页面:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, Session["username"].ToString(), DateTime.Now,
DateTime.Now.AddMinutes(30),
false, Session["username"].ToString()); string cookieStr = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieStr);
cookie.HttpOnly = true;
cookie.Path = FormsAuthentication.FormsCookiePath;
//cookie.Expires = ticket.Expiration;
Context.Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage(Session["username"].ToString(), false);
web.Config:
<authentication mode="Forms">
<forms name="ASPXFORMSAUTH" loginUrl="login.aspx" timeout="20" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<sessionState mode="InProc" timeout="20" cookieless="UseCookies" />//=true
index.aspx.cs:
if (FormsAuthentication.FormsCookieName.ToString() == null)
{
Response.Redirect("login.aspx");
}
else
{
FormsAuthenticationTicket Ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
Response.Write(Ticket.Expiration.ToString());//这里输出的时间是在当前时间上加20分钟!
Session["username"] = Ticket.Name;
}
解决方案 »
- 求高人看看下面这条代码有什么问题吗
- 请教div 的显示问题,zIndex属性……
- ADO.NET事务问题
- 关于repeater的问题
- webservice 中函数的返回类型为xmlDocument,为什么客户端调用的时候返回是xmlnode?怎样与datagrid绑定?
- 如何在javascript中判断服务器控件ListBox的item被选中?
- 俺想写了个自定义控件,是从DataGrid继承的,为啥不行!望大家给看看哦!
- 我的asp.net的网页在自己的机子上面怎么无法显示???
- 有关datagrid的,解决即散分
- 求助错误处理:当前上下文中不存在名称“Strings”
- 谁能吧这个代码调试通 ,asp.net gdi+
- 关于STATIC的大量使用
这句可以不要的!
看看我写的博客吧《ASP。NET安全认证1~4》,不过,图片链接被 CSDN 相册改了
http://blog.csdn.net/cityhunter172
-----------------------------------------------
//过不过期,主要是cookie.Expires的时间,但是你把它注释掉了
//例如加一个CheckBox,选中它30天可以不登陆
if (cbCookie.Checked)//cbCookie是一个CheckBox的id
{
cookie.Expires = DateTime.Now.AddDays(30);
}
false:表示你的cookies不是持久的。改为true就好了
http://www.weste.net/2005/9-30/132630702.html 这文章你看下