You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Impersonation/UserImpersonationExecutor.cs

48 lines
1.3 KiB

using System;
namespace ZeroLevel.Services.Impersonation
{
/// <summary>
/// Class executing code with the rights of the specified user
/// </summary>
public class UserImpersonationExecutor
: IImpersonationExecutor
{
private string USR { get; set; }
private string DOMAIN { get; set; }
private string PWD { get; set; }
private ImpersonationNativeMethods.LogonType logonType = ImpersonationNativeMethods.LogonType.LOGON32_LOGON_INTERACTIVE;
public UserImpersonationExecutor(string userName, string domainName, string password)
{
USR = userName;
DOMAIN = domainName;
PWD = password;
}
/// <summary>
/// Code execution
/// </summary>
public void ExecuteCode<T>(Action<T> action, T arg)
{
using (Impersonation imp = new Impersonation())
{
imp.ImpersonateByUser(USR, DOMAIN, PWD, logonType);
action(arg);
}
}
/// <summary>
/// Code execution
/// </summary>
public void ExecuteCode(Action action)
{
using (Impersonation imp = new Impersonation())
{
imp.ImpersonateByUser(USR, DOMAIN, PWD, logonType);
action();
}
}
}
}

Powered by TurnKey Linux.