使用cookie获取用户信息,长时间不用重新登录
在login页面:
SaveCookie(FormsAuthentication.GetAuthCookie(model.UserName, false), 90)
public static void SaveCookie(HttpCookie cookie, int expireDays)
{
string cookieDomain = "";
string currentURL = HttpContext.Current.Request.Url.AbsoluteUri;
Regex matchNotTLD = new Regex("[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if ((matchNotTLD.IsMatch(cookieDomain)) && (currentURL.IndexOf(cookieDomain) > -1))
{
cookie.Path = "/";
cookie.Domain = cookieDomain;
}
cookie.Expires = DateTime.Now.AddDays(expireDays);
HttpContext.Current.Response.Cookies.Add(cookie);
}
在HttpModule : IHttpModule中获取cookie
if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
HttpContext.Current.User.Identity.Name;
......}
在本机用visual2008调试时, HttpContext.Current.User.Identity.Name隔天都可以获取用户信息,但是发布到服务器空间时,过几十分钟后就获取不到了,必须重新登录。
SaveCookie(FormsAuthentication.GetAuthCookie(model.UserName, false), 90)
public static void SaveCookie(HttpCookie cookie, int expireDays)
{
string cookieDomain = "";
string currentURL = HttpContext.Current.Request.Url.AbsoluteUri;
Regex matchNotTLD = new Regex("[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if ((matchNotTLD.IsMatch(cookieDomain)) && (currentURL.IndexOf(cookieDomain) > -1))
{
cookie.Path = "/";
cookie.Domain = cookieDomain;
}
cookie.Expires = DateTime.Now.AddDays(expireDays);
HttpContext.Current.Response.Cookies.Add(cookie);
}
在HttpModule : IHttpModule中获取cookie
if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
HttpContext.Current.User.Identity.Name;
......}
在本机用visual2008调试时, HttpContext.Current.User.Identity.Name隔天都可以获取用户信息,但是发布到服务器空间时,过几十分钟后就获取不到了,必须重新登录。
...全文
相关推荐
vue通过cookie获取用户登录信息的思路详解
思路 进入页面 若未登录,跳转至登陆页面 若已登录,从cookie中获取用户信息,并执行后续操作 2. 登录页面,存入cookie(setCookie) import {setCookie,getCookie}from 'src/js/cookieUtil' methods: { async cheack_n_p () { if( this.checkCode === this.pwd) { this.loginData = await getUserInfo(this.uname, this.pwd, this.adminPhone); if (th
Android下登录Demo(使用了读写Cookie操作)
Android中读写Cookie实例,包含两个demo:登录时将服务端Cookie写入本地文件,访问列表时附带本地Cookie去请求列表,并打印出来。
JAVA通过Session和Cookie实现网站自动登录
java通过session和cookie实现网站的自动登录
python爬虫使用cookie登录详解
主要介绍了python爬虫使用cookie登录详解,具有一定借鉴价值,需要的朋友可以参考下
网页爬虫之cookie自动获取及过期自动更新的实现方法
主要介绍了网页爬虫之cookie自动获取及过期自动更新的实现方法,需要的朋友可以参考下


