tags:

views:

300

answers:

3

I'm using Forms Authentication (and the built in Login controls) and letting users click the "remember me" checkbox when logging in. When they return to the site, they are automatically logged in as expected. Is there an event I can catch when this happens? I've been studying the FormsAuthenticationModule.Authenticate event, but I'm not sure that is the correct approach.

A: 

First of all, look into using the Login control that's available in 2.0+ (as I remember). There is a Authenticate event you can handle on that control, which is where you can set your session variable.

Additionally, let's assume after authenticating the user is taken to some "home" page. So you can set your session variable inside the "home" page's Load event handler.

Kon
Thanks. I am using the 2.0 Login control. It is only used if the user needs to be prompted, however. If their credentials are stored and they are automatically authenticated, the Login control event isn't around.
Jeremy Mullin
+2  A: 

You might look at Session_Start in global.asax and see if you can detect at that point if the user is authenticated and, if so, set your variable from there. You'll probably need to reference the session from HttpContext.Current.Session (similarly with User). You might also want to look at an HttpHandler that registers an event handler for OnAuthenticate and check/set your variable there.

tvanfosson
+5  A: 

Look in the Application_AuthenticateRequest event in Global.asax.

http://msdn.microsoft.com/en-us/library/system.web.httpapplication.aspx

This event fires after the user has been authenticated (via having manually signed in or with a cookie). From there, you can do what you need. You can interrogate the Page.User object for info about the logged in user,

http://msdn.microsoft.com/en-us/library/system.web.ui.page.user.aspx

rp