Website user logon authentication and user logon authentication
After a cookie is logged on, the website under the same domain name remains in the same logon status.
Login
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) // whether it has been valid for the set expiration time {cookie = new HttpCookie (FormsAuthentication. formsCookieName, ticketEncrypted) {HttpOnly = true, Path = FormsAuthentication. formsCookiePath, Secure = FormsAuthentication. requireSSL, Expires = ticket. expiration, Domain = "cnblogs.com" // set the authenticated Domain name here. The same Domain name includes sub-Domain names such as aa.cnblogs.com or bb.cnblogs.com with the same logon status };} else {cookie = new HttpCookie (FormsAuthentication. formsCookieName, ticketEncrypted) {HttpOnly = true, Path = FormsAuthentication. formsCookiePath, Secure = FormsAuthentication. requireSSL, // Expires = ticket. expiration, // if there is no Expiration time, Domain = "cnblogs.com"};} HttpContext will expire after the browser is closed. current. response. cookies. remove (FormsAuthentication. formsCookieName); HttpContext. current. response. cookies. add (cookie );
}
In this way, the user status can be obtained on any page under the same domain name after logon.
Determine whether a user is logged on
public bool IsAuthenticated{ get { bool isPass = System.Web.HttpContext.Current.User.Identity.IsAuthenticated; if (!isPass) SignOut(); return isPass; }}
Get the current user name
public string GetCurrentUserId(){ return _httpContext.User.Identity.Name;}