views:

63

answers:

2

ASP.NET MVC 2.0, here's my auth code:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(string username, string password, string returnUrl) {
    if (ModelState.IsValid) {
        // Attempt to login
        var loginSuccessful = provider.ValidateUser(username, password);

        if (loginSuccessful) {
            FormsAuthentication.SetAuthCookie(username, true);
            if (!String.IsNullOrEmpty(returnUrl))
                return Redirect(returnUrl);


            return RedirectToAction("Index", "Home");

        }
    }
    return View(Language + "/Login", Vd);
}

Pretty much straight default authentication. Works fine for logging in. However, IE users get auto logged off randomly, even while they're active on the site. Other browsers work fine. Here's the forms auth from web.config:

<authentication mode="Forms">
    <forms loginUrl="~/en/Account/Login" timeout="2880"/>
</authentication>

Where do I begin to look in this case? Have I found a bug?

+1  A: 

As far as I can see everything seems fine, however, could your issue be something to do with your use of a persistent cookie? I think persistent cookies are not meant to timeout, which is why you might be using them.

Try using a non-persistent one instead, and see if that works:

FormsAuthentication.SetAuthCookie(username, false);

Also, a few others notes of interest:

  • I think that the timeout attribute in a web.config is specified in minutes. You've specified more than 2000 minutes.
  • By default, sliding expiration is disabled, so after n minutes it will timeout anyway. If this isn't what you want, then add a slidingExpiration="true" entry onto your <forms/> element in the web.config.
Tim Roberts
Are there any downsides to slidingExpiration? Perhaps performance? Not sure...
Chad
Hi Chad - No, it shouldn't have any impact on performance.To be clear though, with it enabled, the user has n minutes after each request before their session will timeout. With it disabled, they have n minutes from their very first request. Looking back at my answer, I think you could still use a persistent cookie (so the session will be preserved if the user closes the browser), along with a slidingExpiration.I was wondering whether your problem was caused by either using a persistent cookie, or by having such a high number for the timeout.Hope this is making sense!
Tim Roberts
Thanks for your answer. It turns out it was machinekey related, but I made some changes based on your answer too. Much appreciated.
Chad
A: 

What kind of session mode are you using-in process or out of process? If you are using in process with non-persistent cookie and the application pool recycles, then session is lost.

nitroxn