简体   繁体   中英

NLog: Log the user who called the application in a WCF application in IIS

I have the following scenario:

  • A deployed WCF service running as windows hosted service. This WCF service is used to log specific events to a database (NLog v2). This service uses basicHttpBinding.

  • A deployed web application (different server) under IIS. The Application pool runs under NETWORK SERVICE credentials. The web application authentication is Windows-integrated (Active Directory credentials).

Whenever I call the WCF from a console application I can get the user identity from System.Threading.Thread.CurrentPrincipal.Identity.Name ; when I call it from the web application I expect to get "DOMAIN\\SERVER$" as the thread identity name (which occurs as expected).

My problem is: I'd like to get the user who called the web application. I've already tried to set the AspNetCompatibilityRequirements attribute in the WCF service contract, but the ${asp-application} , ${aspnet-user-identity} and ${asp-request} parameters in NLog are read as null . The HttpContext in the web application is OK, but it is not being sent to the WCF. There's a Credentials property in the NLog.Logger class, but it's read-only.

Does anyone know how to solve it?

Probably, you need to set impersonateCallerForAllOperations attribute

See MSDN for details: http://msdn.microsoft.com/en-us/library/ms731306.aspx

If you could get the user from code, you could do this:

using NLog.LayoutRenderers;

...

// Register ${myUser}, do this as soon as possible (e.g. app_start)
LayoutRenderer.Register("myUser", (logEvent) => someUser);

Now you could use ${myUser} in your nlog config1

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