NLog / NLog.WindowsIdentity

NLog extensions for displaying User Windows Identity and target wrapper for user impersonation
BSD 3-Clause "New" or "Revised" License
1 stars 2 forks source link

Unable to update ImpersonatingWrapper Password #6

Closed ABaldeck closed 9 months ago

ABaldeck commented 9 months ago

Hi

Please keep / fill in the relevant info from this template so that we can help you as best as possible.

NLog version: (e.g. 5.2.0)

Platform: .NET7

Current NLog config (xml or C#, if relevant)

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="info"
      internalLogFile="G:\logfiles\inetpub\loop\internal\internal-nlog.txt">
<variable name ="password" value="test"/>
<targets>
<target xsi:type="ImpersonatingWrapper"
      name="wrapper_loop_app"
      userName="********"
      password="${var:password}"
      revertToSelf="false"
      impersonationLevel="Impersonation"
      domain="TESTBED"
      logOnType="Interactive"
      logOnProvider="Default">
<target xsi:type="File"
        name="application"
        fileName="\\*********\Api\${replace:inner=${aspnet-appbasepath}:searchFor=(^(.*?)\ws-usi-loop-api-):replaceWith=:regex=true}\${iis-site-name}\******.${shortdate}.log"
        layout="${longdate} 
| ${pad:padding=5:inner=${level:uppercase=true}}
| ${machinename}
| ${callsite} 
| ${message} ${onexception:${newline}${exception:format=shortType,method,stacktrace:separator=;:maxInnerExceptionLevel=10:innerExceptionSeparator=&#xD;&#xA;&#x9;:innerFormat=shortType,method,stacktrace}} 
| ${aspnet-mvc-controller}/${aspnet-mvc-action} ${aspnet-request-url}"
        keepFileOpen="true"
        concurrentWrites="true"
        maxArchiveDays ="30"
/>
</target>
<!-- write to the void aka just remove -->
        <target xsi:type="Null" name="blackhole" />
</targets>
    <!-- rules to map from logger name to target -->
    <rules>

        <!--All logs, including from Microsoft-->
        <logger name="*"
                minlevel="Trace"
                writeTo="allfile" />

        <!--Skip Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*"
                minlevel="Trace"
                writeTo="blackhole"
                final="true" />

        <!--App logs-->
        <logger name="*"
                minlevel="Debug"
                writeTo="wrapper_loop_app" />

    </rules>
</nlog>

I want to update the password with a BackgroundTask Service to log in a network drive:

UpdateNasPasswordResponse result = await _settings.UrlGda[0]
    .Replace("{AppId}", _settings.AppId)
    .Replace("{Safe}", _settings.CoffreFort)
    .Replace("{Object}", _settings.Objet).GetJsonAsync<UpdateNasPasswordResponse>();

LoggingConfiguration config = LogManager.Configuration;
config.Variables["password"] = HttpUtility.HtmlEncode(result.Password);
LogManager.KeepVariablesOnReload = true;
LogManager.ReconfigExistingLoggers(true);

I try using variable, gdc and getting directly the target:

UpdateNasPasswordResponse result = await _settings.UrlGda[0]
    .Replace("{AppId}", _settings.AppId)
    .Replace("{Safe}", _settings.CoffreFort)
    .Replace("{Object}", _settings.Objet).GetJsonAsync<UpdateNasPasswordResponse>();

LoggingConfiguration config = LogManager.Configuration;
ImpersonatingTargetWrapper target = config.FindTargetByName<ImpersonatingTargetWrapper>(InfrastructureConsts.Nlog.WrapperTarget);
target.Password = HttpUtility.HtmlEncode(result.Password);
LogManager.ReconfigExistingLoggers(true);

I can log the new password in another class but the targets inside the ImpersonatingWrapper didn't seems to be able to log in our NAS.

If I put the password directly inside the nlog.config everything works fine.

welcome[bot] commented 9 months ago

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

snakefoot commented 9 months ago

Created pull-request #5 to add Layout support for UserName / Domain / Password for ImpersonatingWrapper.

snakefoot commented 9 months ago

NLog.WindowsIdentity ver. 5.3 has been released with Layout-support for UserName + Domain + Password:

Thus allowing password="${var:password}"

ABaldeck commented 9 months ago

Thanks a lot!