views:

356

answers:

4

I am running into an issue with ASP.NET MVC where it is forcing the user to log back in after about 20 mins of inactivity.

I am using Forms Authentication and have increased the time-out in the config file as:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="9999999" />
</authentication>

I am also setting the session time-out in the config file as:

<sessionState timeout="120"></sessionState>

I am basing this off of Rockford Lhotka's CSLA ASP.NET MVC example and have the following in my global.asax:

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (HttpContext.Current.Handler is IRequiresSessionState)
        {
            if (Csla.ApplicationContext.AuthenticationType == "Windows")
                return;
            System.Security.Principal.IPrincipal principal;
            try
            {
                principal = (System.Security.Principal.IPrincipal)
                    HttpContext.Current.Session[MyMembershipProvider.SESSION_KEY];
            }
            catch
            {
                principal = null;
            }
            if (principal == null)
            {
                if (this.User.Identity.IsAuthenticated && this.User.Identity is FormsIdentity)
                {
                    // no principal in session, but ASP.NET token
                    // still valid - so sign out ASP.NET
                    FormsAuthentication.SignOut();
                    this.Response.Redirect(this.Request.Url.PathAndQuery);
                }
                // didn't get a principal from Session, so
                // set it to an unauthenticted PTPrincipal
                BusinessPrincipal.Logout();
            }
            else
            {
                // use the principal from Session
                Csla.ApplicationContext.User = principal;
            }
        }
    }

From what I can tell it should ONLY time-out after 120 minutes of inactivity ... but for some reason it always seems to time-out after 20 minutes of inactivity. I have know idea why this is happening, any ideas?

I am toying with the idea of just dumping Forms Authentication and handling it myself via Session, but I'm afraid I would lose functionality like [Authorize] attributes and so on. Trying not to go down this path.

Is it possible to store my custom principal object as a cookie? I just don't want to have to authenticate/authorize a user for every single page or action.

I'm losing hair ... rapidly! =)

+2  A: 

Handling it via Session may not be enough. Because it could be IIS recycling your application, therefor causing all the sessions to be abandoned.

See

çağdaş
That's a great point! So just because I have time-out set in the cookie and in the the session state ... the App Pool could be recycling it sooner? Is that possible?
mattruma
Is there anyway to test to see which is causing the time-out? I'm assuming it the Session.
mattruma
@mattruma, As far as I know, it's possible that App Pool could be recycling it sooner. But I don't know how to test it. IIS7 has a recycling settings for the App Pools. Maybe you should try experimenting with that.
çağdaş
I just looked and it some crazy 1700+ minutes ... with all the places that session can be stored at, it is probably best to try to put this in a cookie ... what do you think?
mattruma
@mattruma, I've never seen an implementation of storing the Identity object in a cookie. Maybe it would be easier to just implement the "Remember Me" functionality. At least it's a more common technique. Here's what I found on SO : http://stackoverflow.com/questions/244882/what-is-the-best-way-to-implement-remember-me-for-a-website
çağdaş
Also, here is an interesting post on lost session variables and appdomain recycles : http://blogs.msdn.com/tess/archive/2006/08/02/asp-net-case-study-lost-session-variables-and-appdomain-recycles.aspx
çağdaş
@mattruma, I just noticed there's also a `idleTimeout` setting for worker processes, on the advanced settings window (IIS7) of an App Pool. It says "Amount of time a worker process will remain idle before it shuts down". Might worth looking into.
çağdaş
A: 

Are you using the membership provider for authorization also? If so you may want to look at the userIsOnlineTimeWindow attribute. The default for this is also 20 minutes.

klabranche
I am using the membership provider, my own custom implementation ... but nonetheless using it.
mattruma
+2  A: 

Mixing concerns of FormsAuthentication with SessionState is just a bad idea on many levels, as you are noticing from the answers you are getting.

If the information describing your custom principal is small, I would suggest storing it in the UserData member of the forms ticket. That is what it is there for.

Then your custom data, which is only valid with a valid ticket, is stored with the ticket.

Many problems solved and mucho code obviated.

Here is a helper class that can help you with your ticket.

CAVEAT: In practice the max http cookie size is just shy of the official 4k limit and Encryption cuts that in half approximately.

If you can ensure that your ticket, including principal data will fit into <2k you should be good to go. Creating a custom serialization for your principal can help, e.g. name=value pairs works great if your data will cooperate.

Good luck.

Sky Sanders
+1  A: 

Hopefully you got this solved by now, but in case somebody else comes along with the same issues, I've been responsible for debugging some code written using the same template, and here are a few thoughts:

1) The forms ticket has a timeout encoded into its value. Most of the example code out there hard-codes this timeout instead of pulling from the forms auth configuration, so if you're just looking at your web.config everything can look fine but your custom security code is ignoring the web.config value. Look through your code for "new FormsAuthenticationTicket" and see what you are doing for the expiration time.

2) The forms cookie has a timeout set in its cookie value. Some of the example code out there hard-codes this timeout. Look and see if you are setting cookie.Expires on your security cookie. (Custom auth tends to hand-build more code here than you would expect because the FormsAuthentication methods don't expose the make-a-cookie-with-userdata method, and you generally want to use userdata to store a bit of info like roles in)

3) Some clients will not set a cookie on response redirect. And sometimes even if they do, you'll get back a cookie other than the one you set. For example, if you have changed the app path or domain at any point, it's possible for the user to have two valid cookies, and you're only clearing one when you try to log them back in here. Now, this code basically reads "The user has some session info, and was logged in, but their session didn't contain the principal I expected it to, so I redirect them to login again." Well, if they don't listen to your auth cookie, or have an auth cookie you don't expect (maybe you changed your domain or path values at some point and they have a /oldpath cookie still set), this can infinite loop. I recommend nuking the session server-side as soon as you find out that it doesn't have the data you want: Session.Clear() - this leaves you less likely to end up in this situation after a redirect. (From a recover-server-side-without-trusting-the-client-to-behave perspective, it's actually slightly safer to go ahead and reconstruct the principal object and put it into the session, but I can see how this would be less secure.)

It's also safer to just do a Server.Transfer to the login page rather than relying on a cookie-changing redirect to work right. If you do end up in a redirect loop, server.transfer is guaranteed to end it.

David Eison