Boppster
Boppster

Reputation: 48

.NET Get logged in user

My problem is that I have a asp.net solution that uses a seperate project for logging and now I want the log to save the logged in user. Windows authentication is used for the webapp and the app pool is run under a service account. So when I call the logger through a method(static) I want the logger to pick up who is the logged in user. The only thing I currently get is the service account that the app pool run under.

Any ideas of how to get the user from the webapp?

Upvotes: 0

Views: 812

Answers (3)

Devdutt Patnaik
Devdutt Patnaik

Reputation: 71

Make sure that your thread principal is the same as the System.Web.HttpContext.Current.User

At the end of the Application_AuthenticateRequest method in your global.asax, you may need something like:

System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;

Upvotes: 2

Chris
Chris

Reputation: 27599

You might be able to use HttpContext.Current.User.Identity.Name. I think that HttpContext.Current should be correctly initialised even in a separate project.

Upvotes: 2

TomTom
TomTom

Reputation: 62093

Yes. Not from the logger, obviously. You need to acess the HttpRequest somehow - in the page etc.

So, it is doable in a logging framework embedded in the application, it is not doable from a different level so much.

The user will be recorded in request level properties.

Upvotes: 0

Related Questions