Use User.Identity.Name when the user first hits the site

I want to run code that uses User.Identity.Name,

I want to run this code when the user first logs into the site. (maybe every time they close the browser and then open the site again)

I tried putting it in Application_Start in global.asax, but the name User.Identity.Name returned null;

How can i achieve this?

+4
source share
2 answers

Based on your description, you probably want to put your code in Session_Start, and not in Application_Start. Session_Start is an event that fires when your application first sees each user.

Here is a list of events available in Global.asax: http://msdn.microsoft.com/en-us/library/system.web.httpapplication_events%28v=VS.100%29.aspx

+7
source

User.Identity.Name will not be empty only when the user is authenticated. If you use Forms Authentication , this will happen when you call FormsAuthentication.SetAuthCookie . If you use integrated Windows authentication, then usually the user will be automatically authenticated when he first requests the site. Normally you should not try to access it in Application_Start . You can write your own Application_AuthenticateRequest authorization method, which runs when the security module identifies the user.

+6
source

All Articles