Pregunta

Necesito poder ejecutar RegLoadKey () en una máquina remota, y puede ser que mi máquina y la máquina remota no estén en el mismo dominio. Si lo están, el siguiente código funciona bien y puedo suplantar a un usuario que tiene privilegios de administrador en la máquina. De lo contrario, si estamos hablando de usuarios locales, de acuerdo con esta discusión, encontré ...

http://www.eggheadcafe.com/conversation.aspx?messageid = 34224301 & amp; threadid = 34224226

... Tiene que haber un usuario local en mi máquina con el mismo nombre de usuario y contraseña. Ugh ¿Hay alguna forma de evitar eso?

using System.Runtime.InteropServices;
using System.Security.Principal;

[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

public WindowsImpersonationContext WearDrag(string Username, string Password, string DomainOrMachine)
{
    WindowsImpersonationContext impersonationContext;
    WindowsIdentity tempWindowsIdentity;
    IntPtr token = IntPtr.Zero;
    IntPtr tokenDuplicate = IntPtr.Zero;

    if (RevertToSelf())
    {
        if (LogonUserA(Username, DomainOrMachine, Password,
            LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT, ref token) != 0)
        {
            if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
            {
                tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                impersonationContext = tempWindowsIdentity.Impersonate();
                if (impersonationContext != null)
                {
                    CloseHandle(token);
                    CloseHandle(tokenDuplicate);
                    return impersonationContext;
                }
            }
        }
    }
    if (token != IntPtr.Zero)
        CloseHandle(token);
    if (tokenDuplicate != IntPtr.Zero)
        CloseHandle(tokenDuplicate);
    return null;
}
¿Fue útil?

Solución

Esto es lo que he estado usando sin tener que definir un usuario local:

const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
const int LOGON32_PROVIDER_DEFAULT = 0;

bool isSuccess = LogonUser(username, domain, password,
            LOGON32_LOGON_NEW_CREDENTIALS,
            LOGON32_PROVIDER_DEFAULT, ref token);

Después de eso:

WindowsIdentity newIdentity = new WindowsIdentity(token);
WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate();

Sin embargo, no duplico el identificador.

Otra observación: no uso LogonUserA, simplemente uso LogonUser.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top