使用cookie获取用户信息,长时间不用重新登录

melon23 melon23 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在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隔天都可以获取用户信息,但是发布到服务器空间时,过几十分钟后就获取不到了,必须重新登录。
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
melon23
.NET社区 62,275 成员 668,968 内容