views:

569

answers:

3

I need help figuring out why my authentication ticket is expiring after about an hour on my hosted website. But if I run the site locally the authentication ticket stays alive for the correct amount of time.

This is my localhost web.config:

<authentication mode="Forms">
    <forms loginUrl="~/Account/LogOn" timeout="20160" slidingExpiration="true" path="/" />
</authentication>

and this is my hosted web.config:

<authentication mode="Forms">
 <forms loginUrl="~/Account/LogOn" timeout="20160" slidingExpiration="true" domain=".mywebsite.com" path="/" />
</authentication>

I know the authentication ticket is being created because:

  • I can see it in the browser cookies
  • I stay logged in even after closing the browser and reopening
  • I stay logged in even after website recycles (changing and saving web.config to recycle it)

When I check the cookie expiration date in the browser it's 2 weeks later. However, after about an hour my authentication always expires.

What can I do to figure out why the hosted website's authentication is expiring so early? I don't know how to go about resolving this problem since it's my hosted website that is the only one having problems.

Update 1: After waiting 1 hour, I check my browser and I see the cookie still exists. In fact it's expiration date is set for 2 weeks later. But if I reload the page or try going to any pages that requires authentication I am taken to the login page.

A: 

I would try several things in troubleshooting this:

  • IIS version & settings between your localhost & hosting. Most likely there are some differences in application pool setting
  • In IIS 7, there is a special setting for this: read here
Johannes Setiabudi
I couldn't tell from the link for IIS 7 but does this override the settings I specify in my web.config?
codette
I am not sure. but you probably want to try to config the IIS
Johannes Setiabudi
A: 

Have you asked your hosting provider if the machine.config has this set to a diferent value? Settings on machine.config will override the web.config.

Radu094
Yes they showed me the machine.config and I don't see any authentication section in there. So I am assuming that means my web.config values are what's taking effect.
codette
+1  A: 

I added a machinekey entry in system.net. Something like this:

 <machineKey validationKey="aaa"
 decryptionKey="bbb" validation="SHA1" />

and now it keeps the user logged in. However, now it seems like I am having performance issues. The page used to take roughly 500ms to load now takes about double that time.

codette
I had exactly the same problem and this fixed it. I specified only the validationKey and it did the trick without performance penalty. The only explanation I can come up with is that the auto-generated key (which is the default if you don't specify an explicit key) is changing for some reason every now and then.Because the documentation of this seems to be really poor, here's how to do it: Add the <machineKey> tag inside web.config's <system.web> and set the validationKey to a random string of hexadecimal characters (0-9 and A-F). The total string length should be exactly 128 characters.
smt
My joy was premature. The problem persists, so adding an explicit validationKey didn't help after all. No idea how to fix this other than dumping the whole FormsAuthentication stuff and implementing a custom authentication system.
smt