Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make use of User.Identity.Name when user first hits the site

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?

like image 593
raklos Avatar asked Dec 13 '22 10:12

raklos


2 Answers

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

like image 58
Cory Grimster Avatar answered Mar 03 '23 09:03

Cory Grimster


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.

like image 45
Darin Dimitrov Avatar answered Mar 03 '23 10:03

Darin Dimitrov