ASP.NET在页面之外获取Windows用户名(ASP.NET get windows username outside of page)

编程入门 行业动态 更新时间:2024-10-22 20:30:49
ASP.NET在页面之外获取Windows用户名(ASP.NET get windows username outside of page)

我有一个使用Windows身份验证的现有ASP.NET报告应用程序。 很多报告生成代码都在单独的类中,并且有一个我没有写入的核心错误记录器,我相信这个错误记录器是为Windows应用程序构建的,因为它使用WindowsIdentity.GetCurrent()。Name。 在ASP.NET的情况下,我相信这将返回在服务器上运行ASP.NET页面的帐户。

我相信在页面上使用User.Identity.Name将是正确的方式,但它不能从仅在页面上的报告生成类中提供。 有没有办法使用错误记录器类来获取它,而不将它作为一个额外的参数传递。

有数百个报告类,所以我害怕必须通过并为每一个添加一个参数。

I have an Existing ASP.NET reporting application using windows authentication. 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. In the case of ASP.NET I believe this will return the account running the ASP.NET pages at the server.

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. 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.

最满意答案

如果你可以在你的web.config中使用模拟:

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

您的报告课程将获得正确的用户。

如果您的报告类可以引用System.Web程序集并且您愿意修改代码,则还可以执行以下操作:

HttpContext.Current.User.Identity.Name

但要确保调用者来自ASP.NET请求,否则会抛出一个nullref。

If you can use impersonation in your 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:

HttpContext.Current.User.Identity.Name

but make sure the caller comes from an ASP.NET request or it will throw a nullref.

更多推荐

ASP,NET,Name,报告,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-07 16:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465078.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户名   页面   ASP   NET   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!