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

53 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
namespace ZeroLevel.Services.Impersonation
{
/// <summary>
/// Класс исполняющий произвольный код с правами указанного пользователя
/// </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>
/// Исполнение кода
/// </summary>
/// <typeparam name="T">Тип передаваемого аргумента</typeparam>
/// <param name="action">Делегат</param>
/// <param name="arg">Аргумент</param>
public void ExecuteCode<T>(Action<T> action, T arg)
{
using (Impersonation imp = new Impersonation())
{
imp.ImpersonateByUser(USR, DOMAIN, PWD, logonType);
action(arg);
}
}
/// <summary>
/// Исполнение кода
/// </summary>
/// <typeparam name="T">Тип передаваемого аргумента</typeparam>
/// <param name="action">Делегат</param>
public void ExecuteCode(Action action)
{
using (Impersonation imp = new Impersonation())
{
imp.ImpersonateByUser(USR, DOMAIN, PWD, logonType);
action();
}
}
}
}

Powered by TurnKey Linux.