1

Here's how I have authentication configured in web.config:

<authentication mode="Forms">
      <forms loginUrl="~/Account/Sigin" 
             name="MYCAUTH" 
             timeout="3000"  />
</authentication>

How can I make both MYCAUTH and ASP.NET_SessionId cookies use expiration?

JamesL
  • 351
  • 2
  • 10

2 Answers2

1
    var myCookie = Request.Cookies["myCookie"];
    if (myCookie != null)
    {
        HttpCookie respCookie = new HttpCookie("myCookie", "MyValue");
        respCookie.Expires = DateTime.Now.AddMinutes(5);
        Response.Cookies.Set(myCookie);
    }
Ram
  • 504
  • 3
  • 11
1

Try this:

DateTime expireDate = DateTime.Now.AddDays(30);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, expireDate, true, string.Empty);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authenticationCookie.Expires = ticket.Expiration;
Response.Cookies.Add(authenticationCookie);
FormsAuthentication.SetAuthCookie(userName, true);
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • thanks. is it possible to do with `FormsAuthentication.SetAuthCookie`? Also please note I updated my question. I need expiration for both `ASP.NET_SessionId` and `MYCAUTH` – JamesL Oct 12 '16 at 12:33
  • `ASP.NET_SessionId` will be expired as soon as you close the browser. Aand those ar 2 different things anyway. See this post for more info http://stackoverflow.com/questions/4939533/cookie-confusion-with-formsauthentication-setauthcookie-method – VDWWD Oct 12 '16 at 13:01