login control - keep me login until I logout
来源:互联网 发布:淘宝玛卡是真的吗 编辑:程序博客网 时间:2024/06/05 17:07
in the login control event loggedIn
protected void LoginControl_OnLoggedIn(object sender, EventArgs e) { CheckBox cb = LoginControl.FindControl("RememberMe") as CheckBox; if (cb != null && cb.Checked) { string userData = "keep user login"; // true if a durable cookie (a cookie that is saved across browser sessions) was issued; otherwise, false. bool isPersistent = false; string username = LoginControl.UserName; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddYears(1), isPersistent, userData, FormsAuthentication.FormsCookiePath); // Encrypt the ticket. string encTicket = FormsAuthentication.Encrypt(ticket); // Create the cookie. //Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); // Create the cookie. HttpCookie cook = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); cook.Expires = DateTime.Now.AddYears(1); // if not set the expire date, the cookie will removed after browser closed Response.Cookies.Add(cook); } }
// get the how many minutes total and left to logout, form authentication
public class FormsAuthenticationHelper { public static double GetFormTimeout(Page page) { HttpCookie cookie = (HttpCookie)(page.Request.Cookies[FormsAuthentication.FormsCookieName]); // if no user login, the cookie will be null if (cookie != null) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); double timeoutInMinutes = (ticket.Expiration - ticket.IssueDate).TotalMinutes; return timeoutInMinutes; } return -1; } public static double GetTotalLeftFormTimeout(Page page) { HttpCookie cookie = (HttpCookie)(page.Request.Cookies[FormsAuthentication.FormsCookieName]); // if no user login, the cookie will be null if (cookie != null) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); if (ticket.Expiration - DateTime.Now > 0) { double timeoutMillisecond = (ticket.Expiration - DateTime.Now).TotalMilliseconds; return timeoutMillisecond; } } return -1; } }
// how to use
double currentTimeout = FormsAuthenticationHelper.GetFormTimeout(this.Page); if (currentTimeout <= 60 * 24 * 30 && currentTimeout > -1) // if the page has timeout in a month your function();
- login control - keep me login until I logout
- Login, logout
- login logout
- Audit login 与 Audit logout
- Linux 下 Login 和 Logout 详解
- Laravel 自动生成验证分析:login / logout
- login
- Login
- Login
- login
- Login
- login
- login
- login
- login
- login
- login
- Login
- 网络资源下载
- sgu 137 Funny Strings
- cglib 介绍 原理 及 使用
- 五种方法查看Ubuntu/Redhat等Linux系统版本号等系统信息
- 2012年的移动互联网市场
- login control - keep me login until I logout
- 地摊砍价
- MVC中使用AuthorizeAttribute做身份验证操作
- 配置ehcache.xml文件
- HDU 4295 状态压缩dp + KMP
- 配置ehcache.xml -----02
- 分段函数
- 一个问题求解
- 匿名内部类的神奇用法——实现视图机制