2010-02-17 31 views
5

Estoy trabajando en una aplicación ASP.net y yo estoy tratando de hacerse pasar por un usuario¿Cómo suplantar a otro usuario?

Estoy creando un WindowsIdentity con un token

WindowsIdentity winId = new WindowsIdenty(token); 

esta muestra se obtuvo mediante una llamada al logrado ONU código

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

¿hay alguna otra manera de obtener un token sin el uso de este código no administrado advapi32.dll?

TKS

+0

hay más formas de API Win32. –

Respuesta

3

Personalmente, prefiero un wrapper class para manejar esta suplantación.

Por lo tanto, trabajará con código no administrado, pero AFAIK no hay forma de hacerlo directamente con el código administrado.

+0

Su código de clase de contenedor funcionó de maravilla. Cortar y pegar ¡Gracias! –

2

construido una clase: Impersonate.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Security; 
using System.Security.Principal; 
using System.Runtime.InteropServices; 
using System.IO; 
using System.Text; 

using System.Web; 

// you must change the YourProgramName 

namespace [YourProgramName] 
{ 
    public class Impersonate 
    { 

     [DllImport("advapi32.dll", SetLastError = true)] 
     private static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, 
              int dwLogonType, int dwLogonProvider, out int phToken); 

     [DllImport("kernel32.dll")] 
     private static extern int FormatMessage(int dwFlags, string lpSource, int dwMessageId, int dwLanguageId, 
               StringBuilder lpBuffer, int nSize, string[] Arguments); 


     private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8; 
     private const int LOGON32_PROVIDER_DEFAULT = 0; 
     private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000; 

     private static WindowsImpersonationContext winImpersonationContext = null; 

     public static void ImpersonateUser(string domain, string userName, string password) 
     { 

      //Benutzer einloggen 
      int userToken = 0; 

      bool loggedOn = (LogonUser(userName, domain, password, LOGON32_LOGON_NETWORK_CLEARTEXT, 
             LOGON32_PROVIDER_DEFAULT, out userToken) != 0); 

      if (loggedOn == false) 
      { 
       int apiError = Marshal.GetLastWin32Error(); 
       StringBuilder errorMessage = new StringBuilder(1024); 
       FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, null, apiError, 0, errorMessage, 1024, null); 
       throw new Exception(errorMessage.ToString()); 
      } 

      WindowsIdentity identity = new WindowsIdentity((IntPtr)userToken); 
      winImpersonationContext = identity.Impersonate(); 

     } 

     public static void UndoImpersonation() 
     { 
      if (winImpersonationContext != null) 
      { 
       winImpersonationContext.Undo(); 
      } 
     } 

    } 
} 

uso en su programa:

Impersonate.ImpersonateUser("Domain", "Username", "UserPassword"); 

        //Your Code as the new User 

       Impersonate.UndoImpersonation(); 
Cuestiones relacionadas