The cookie logs on to the same login status as the website under the domain name.
Login
private void SetAuthCookie (string userId, bool createPersistentCookie)
{
varTicket =NewFormsAuthenticationTicket (2, UserId, DateTime.Now, DateTime.Now.AddDays (7),true,"", Formsauthentication.formscookiepath); stringticketencrypted =Formsauthentication.encrypt (ticket); HttpCookie cookies; if(createPersistentCookie)//Whether it is valid for the set expiration period {cookie=NewHttpCookie (Formsauthentication.formscookiename, ticketencrypted) {HttpOnly=true, Path=Formsauthentication.formscookiepath, Secure=Formsauthentication.requiressl, Expires=ticket. Expiration,Domain ="cnblogs.com"//Here Set the authentication domain name, with the domain name including sub-domain name, such as aa.cnblogs.com or bb.cnblogs.com to keep the same login status}; }Else{Cookie=NewHttpCookie (Formsauthentication.formscookiename, ticketencrypted) {HttpOnly=true, Path=Formsauthentication.formscookiepath, Secure=Formsauthentication.requiressl,//Expires = ticket. expiration,//No expiration time, the browser is closed after the failureDomain ="Cnblogs.com"}; } HttpContext.Current.Response.Cookies.Remove (Formsauthentication.formscookiename); HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookie);
}
The user status can be obtained from any page under the same domain name after login
Determine if the user is logged on
Public BOOL isauthenticated{ get { bool ispass = System.Web.HttpContext.Current.User.Ide Ntity. isauthenticated; if (! Ispass) SignOut (); return Ispass; }}
Get the current user name
Public string Getcurrentuserid () { return _httpcontext.user.identity.name;}
Site User Login Authentication