Site User Login Authentication

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.