#58 Add <credential> to <exec> task to run as user

open
nobody
Tasks (29)
5
2010-12-08
2010-12-08
Anonymous
No

The code below adds support for a <credential> element under the <exec> task, which allows the process to be executed as a different user. Could you please add it to NAnt.Core.Tasks.ExecTask?

#region Private Instance Fields

private Credential _credential;

#endregion Private Instance Fields

#region Public Instance Properties

[NAnt.Core.Attributes.BuildElement(\"credential\", Required = false)]
public Credential Credential
{
get { return _credential; }
set { _credential = value; }
}

#endregion Public Instance Properties

#region Protected Instance Methods

/// <summary>
/// Updates the <see cref=\"ProcessStartInfo\" /> of the specified
/// <see cref=\"Process\"/>.
/// </summary>
/// <param name=\"process\">The <see cref=\"Process\" /> of which the <see cref=\"ProcessStartInfo\" /> should be updated.</param>
protected virtual void PrepareProcess(Process process)
{
... // Existing code for PrepareProcess goes here.

if (_credential != null)
{
process.StartInfo.Domain = _credential.Domain;
process.StartInfo.UserName = _credential.UserName;
process.StartInfo.Password = new System.Security.SecureString();
foreach (char ch in _credential.Password)
process.StartInfo.Password.AppendChar(ch);
}
}

#endregion Protected Instance Methods

Discussion