I want to run some code that makes use of User.Identity.Name,
I want to run that code when the user first enters the site. (can be everytime they close the browser then reopen the site)
i tried putting it in Application_Start in the global.asax, but the User.Identity.Name name was returning null;
How can I achieve this?
Based on your description, you probably want to put your code in Session_Start, not Application_Start. Session_Start is the event that fires when your application first sees each user.
Here's the list of events that are available in Global.asax: http://msdn.microsoft.com/en-us/library/system.web.httpapplication_events%28v=VS.100%29.aspx
User.Identity.Name
will not be null only when the user is authenticated. If you are using Forms Authentication
this will happen when you call FormsAuthentication.SetAuthCookie
. If you are using Integrated Windows Authentication then normally the user will be automatically authenticated when he first requests the site. Normally you should not try to access it in Application_Start
. You could write a custom authorization Application_AuthenticateRequest
method which is fired when a security module establishes the identity of the user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With