简体   繁体   English

ASP.NET在页面外获取Windows用户名

[英]ASP.NET get windows username outside of page

I have an Existing ASP.NET reporting application using windows authentication. 我有一个使用Windows身份验证的现有ASP.NET报告应用程序。 A lot of the report generation code is in separate classes and has a core error logger that I didn't write, this error logger I believe was built for windows apps as it uses WindowsIdentity.GetCurrent().Name. 许多报告生成代码位于单独的类中,并且具有我未编写的核心错误记录器,我相信此错误记录器是为Windows应用程序构建的,因为它使用WindowsIdentity.GetCurrent()。Name。 In the case of ASP.NET I believe this will return the account running the ASP.NET pages at the server. 对于ASP.NET,我相信这将返回在服务器上运行ASP.NET页的帐户。

I believe using User.Identity.Name on the pages would be the correct way to do this but it is not available from within the report generation classes only on the page. 我相信在页面上使用User.Identity.Name是执行此操作的正确方法,但仅在页面上的报告生成类中不可用。 Is there a way to obtain it withing the error logger class without passing it as an extra parameter. 有没有一种方法可以通过错误记录器类获取它,而无需将其作为附加参数传递。

There are hundreds of report classes so I dread to have to go through and add a parameter to every one. 有数百种报告类,因此我很害怕必须遍历并为每个类添加一个参数。

If you can use impersonation in your web.config: 如果可以在web.config中使用模拟:

....
<authentication mode="Windows"/>
<identity impersonate="true"/>
....

your report classes will get the right user. 您的报告类别将吸引合适的用户。

If your reporting classes can reference the System.Web assembly and you are willing modify the code, you could also do: 如果您的报告类可以引用System.Web程序集,并且您愿意修改代码,则还可以执行以下操作:

HttpContext.Current.User.Identity.Name

but make sure the caller comes from an ASP.NET request or it will throw a nullref. 但请确保调用方来自ASP.NET请求,否则它将引发nullref。

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

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