星空网 > 软件开发 > ASP.net

网站用户登录认证

cookie登录后同域名下的网站保持相同的登录状态。

登录

private void SetAuthCookie(string userId, bool createPersistentCookie)
{
  var ticket = new FormsAuthenticationTicket(2, userId, DateTime.Now, DateTime.Now.AddDays(7), true, "", FormsAuthentication.FormsCookiePath);  string ticketEncrypted = FormsAuthentication.Encrypt(ticket);  HttpCookie cookie;  if (createPersistentCookie)//是否在设置的过期时间内一直有效  {    cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted)    {      HttpOnly = true,      Path = FormsAuthentication.FormsCookiePath,      Secure = FormsAuthentication.RequireSSL,      Expires = ticket.Expiration,      Domain = "cnblogs.com"//这里设置认证的域名,同域名下包括子域名如aa.cnblogs.com或bb.cnblogs.com都保持相同的登录状态    };  }  else  {    cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted)    {      HttpOnly = true,      Path = FormsAuthentication.FormsCookiePath,      Secure = FormsAuthentication.RequireSSL,      //Expires = ticket.Expiration,//无过期时间的,浏览器关闭后失效      Domain = "cnblogs.com"    };  }  HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);  HttpContext.Current.Response.Cookies.Add(cookie);
}

这样登录后,在同域名下的任何页面都可以得到用户状态

判断用户是否登录

public bool IsAuthenticated{  get  {    bool isPass = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;    if (!isPass)      SignOut();    return isPass;  }}

得到当前的用户名

public string GetCurrentUserId(){   return _httpContext.User.Identity.Name;}

 




原标题:网站用户登录认证

关键词:登录

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

宠物梳专利查询分析:https://www.kjdsnews.com/a/1842293.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流