Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to update ImpersonatingWrapper Password #6

Closed
ABaldeck opened this issue Dec 19, 2023 · 4 comments · Fixed by #5
Closed

Unable to update ImpersonatingWrapper Password #6

ABaldeck opened this issue Dec 19, 2023 · 4 comments · Fixed by #5
Labels
question Further information is requested

Comments

@ABaldeck
Copy link

ABaldeck commented Dec 19, 2023

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.

Copy link

welcome bot commented Dec 19, 2023

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

@snakefoot
Copy link
Contributor

snakefoot commented Dec 19, 2023

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

@snakefoot
Copy link
Contributor

snakefoot commented Dec 22, 2023

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

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

@ABaldeck
Copy link
Author

Thanks a lot!

@snakefoot snakefoot added the question Further information is requested label Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
2 participants