简体   繁体   中英

ASP.NET get windows username outside of page

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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