Implement Remember Me Feature in ASP.NET

What is the best way to implement functionality remember meon an ASP.NET website?

Should I use custom cookies or is there an easier method?

+3
source share
2 answers

Do you use the built-in services Authenicationprovided by ASP.NET? If so, then it is pretty easy.

+3
source

cookie-- ( cookie- asp.net), - , , , . :

public void SetAuthenticationCookie(LoginView loginModel)
    {
      if (!loginModel.RememberMe)
      {
        FormsAuthentication.SetAuthCookie(loginModel.Email, false);
        return;
      }
      const int timeout = 2880; // Timeout is in minutes, 525600 = 365 days; 1 day = 1440.
      var ticket = new FormsAuthenticationTicket(loginModel.Email, loginModel.RememberMe, timeout);
      //ticket.
      string encrypted = FormsAuthentication.Encrypt(ticket);
      var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted)
        {
          Expires = System.DateTime.Now.AddMinutes(timeout),
          HttpOnly = true
        };
      HttpContext.Current.Response.Cookies.Add(cookie);
    }
0

Source: https://habr.com/ru/post/1718825/


All Articles