3

I used this following code to set authenticate cookie :

System.Web.Security.FormsAuthentication.SetAuthCookie(Profile.Email, true);

my question is how I can increase life-time for this authentication cookie ?

kamiar3001
  • 2,646
  • 4
  • 42
  • 78

2 Answers2

7

The timeout is set primarily in the web.config file, you can do it in code but I wouldn't advise it.

These are the default settings, you can see the timeout value that's specified in minutes.

<system.web>
  <authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH" 
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" />
  </authentication>
</system.web>
Lazarus
  • 41,906
  • 4
  • 43
  • 54
0

This is how to set that time up. (For instance, for two weeks expiration).

var cookie = FormsAuthentication.GetAuthCookie("user-name", false);
cookie.Expires = DateTime.UtcNow.AddDays(14);
Response.Cookies.Add(cookie);
Agat
  • 4,577
  • 2
  • 34
  • 62
  • It's a nice solution, but better use `Response.Cookies.Set` over `Add` in order to update the cookie and not adding another one. – ilans Jun 21 '15 at 16:42