简体   繁体   English

.NET获取登录用户

[英].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. 我的问题是我有一个使用单独项目进行日志记录的asp.net解决方案,现在我希望该日志保存已登录的用户。 Windows authentication is used for the webapp and the app pool is run under a service account. Web应用程序使用Windows身份验证,并且应用程序池在服务帐户下运行。 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? 关于如何从网络应用中吸引用户的任何想法?

You might be able to use HttpContext.Current.User.Identity.Name . 您可能可以使用HttpContext.Current.User.Identity.Name I think that HttpContext.Current should be correctly initialised even in a separate project. 我认为HttpContext.Current即使在单独的项目中也应该正确初始化。

Make sure that your thread principal is the same as the System.Web.HttpContext.Current.User 确保您的线程主体与System.Web.HttpContext.Current.User相同

At the end of the Application_AuthenticateRequest method in your global.asax, you may need something like: 在global.asax中Application_AuthenticateRequest方法的末尾,您可能需要以下内容:

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

Yes. 是。 Not from the logger, obviously. 显然不是来自记录器。 You need to acess the HttpRequest somehow - in the page etc. 您需要以某种方式访问​​HttpRequest-在页面等中。

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. 该用户将被记录在请求级别的属性中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM