简体   繁体   English

NLog:记录在 IIS 中的 WCF 应用程序中调用该应用程序的用户

[英]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.作为 Windows 托管服务运行的已部署 WCF 服务。 This WCF service is used to log specific events to a database (NLog v2).此 WCF 服务用于将特定事件记录到数据库 (NLog v2)。 This service uses basicHttpBinding.此服务使用 basicHttpBinding。

  • A deployed web application (different server) under IIS.在 IIS 下部署的 Web 应用程序(不同的服务器)。 The Application pool runs under NETWORK SERVICE credentials.应用程序池在 NETWORK SERVICE 凭据下运行。 The web application authentication is Windows-integrated (Active Directory credentials). Web 应用程序身份验证与 Windows 集成(Active Directory 凭据)。

Whenever I call the WCF from a console application I can get the user identity from System.Threading.Thread.CurrentPrincipal.Identity.Name ;每当我从控制台应用程序调用 WCF 时,我都可以从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).当我从 Web 应用程序调用它时,我希望得到"DOMAIN\\SERVER$"作为线程标识名称(按预期发生)。

My problem is: I'd like to get the user who called the web application.我的问题是:我想获得调用 Web 应用程序的用户。 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 .我已经尝试在 WCF 服务合同中设置AspNetCompatibilityRequirements属性,但是 NLog 中的${asp-application}${aspnet-user-identity}${asp-request}参数被读取为null The HttpContext in the web application is OK, but it is not being sent to the WCF. Web 应用程序中的HttpContext没问题,但没有发送到 WCF。 There's a Credentials property in the NLog.Logger class, but it's read-only. NLog.Logger类中有一个Credentials属性,但它是只读的。

Does anyone know how to solve it?有谁知道如何解决它?

Probably, you need to set impersonateCallerForAllOperations attribute可能,您需要设置impersonateCallerForAllOperations属性

See MSDN for details: http://msdn.microsoft.com/en-us/library/ms731306.aspx有关详细信息,请参阅 MSDN: 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现在您可以在 nlog config1 中使用${myUser}

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

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