Website user logon authentication and user logon authentication

Source: Internet
Author: User

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;}

 

Related Article

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.