I need to revoke an authentication cookie if the user no longer exists (or some other condition), after the forms authentication mechanism already have received the authentication cookie from the browser and have validated it. I.e. here is the use scenario:
- The user have been authenticated, and granted non-expiring auth cookie.
-
In a few days, the user tries to access my web app again, and as the cookie is valid, the forms authentication mechanism will grant access.
-
Now I want to perform a second check (whatever condition I want), and decide if I want to let the user continue, or to revoke the authentication.
The question is – is there an official automated way for this? So far I have come with some possibilities, but I do not know which one is better. I can capture the Authenticate event in global.asax, check whatever I want, and to revoke I clear the cookie, and then one of these:
-
Redirect again to same url – this should work, as this time the forms authentication will fail, and it will redirect to logon page.
-
Throw some exception ??? which one to make the redirect happen w/o me specifying anything?
-
Somehow to get the logon page url from the config file (any ideas how/which config handler to use) and redirect directly?
-
Some FormsAuthentication class/method I have overlooked, which is designed for this?
-
Any other idea?
I don’t think there is an automated way to achive this. I think the best way would be to add a date to the auth cookie which will be the last time you checked whether the user exists. So when a user logs-in you’ll:
Then everytime a user is authenicated you can check the additional date you passed to the Authentication ticket and in 10 minute intervals or less double check against the database whether the user exists. The code might look something like this:
You can even cache the users that have been deleted the last 10 minutes and check against that collection.
Hope that helps.