Network Impersonation Question

From: Derek Hamilton (derek@capweb.com)
Date: 07/25/02


From: "Derek Hamilton" <derek@capweb.com>
Date: Wed, 24 Jul 2002 20:23:17 -0700


I am attempting to impersonate a user and then perform active directory
updates/searches with that identity. I am not having a problem
impersonating the user until I try to do a network action. The below is the
code I'm testing with (sorry about the formatting):

-----Code Block-----
[assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum,
UnmanagedCode=true)]
namespace ImpersonateTest
{
class Class1
{
[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

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

[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
if(c.ValidateUser("user", "domain", "password"))
{
Console.WriteLine("Impersonating User");

Console.WriteLine("Connecting to Directory");
DirectoryEntry objRoot = new DirectoryEntry("LDAP://DC=domain,DC=com");
foreach (DirectoryEntry de in objRoot.Children)
{
Console.WriteLine(de.Path);
foreach (DirectoryEntry de2 in de.Children)
{
Console.WriteLine("" + de2.Path);
}
}
c.impersonationContext.Undo();
}
else
Console.WriteLine("Impersonation failed");
Console.Read();
}

public bool ValidateUser(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if(LogonUser(userName, domain, password, 3, 0, ref token) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
return true;
else
return false;
}
else
return false;
}
else
return false;
}
WindowsImpersonationContext impersonationContext;
}
}
-----End Block----

I found the ValidateUser code in a knowledge base article (sorry, didn't
keep the link) and noticed the DuplicateToken call. The hardcoded value 3
is a value that should set the Security Impersonation level to work on
network resources also.

I'm wondering if any other type of delegation has to happen to access the
network as the correct user?

BTW, I don't get any error messages, the application just ends.

TIA,
Derek Hamilton



Relevant Pages

  • Need help with impersonation, please.
    ... string user = username.Text; ... private static WindowsImpersonationContext impersonationContext; ... IntPtr tokenDuplicate = IntPtr.Zero; ... public static extern int LogonUser( ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Network Impersonation Question
    ... You may want to post this question in another newsgroup, ... | public static extern int LogonUser(String lpszUsername, String lpszDomain, ... | int dwLogonType, int dwLogonProvider, ref IntPtr phToken); ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Network Impersonation Question
    ... Logontype 3 (LOGON32_LOGON_NETWORK) has not network credentials. ... > public static extern int LogonUser(String lpszUsername, String lpszDomain, ... > int dwLogonType, int dwLogonProvider, ref IntPtr phToken); ... > impersonationContext = tempWindowsIdentity.Impersonate; ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Inverse P/Invoke an pointer to pointer
    ... int Configure(IntPtr iu_parentHandle, ref IntPtr iop_config, int ini_size) { ... ANSI - This uses LocaAlloc to allocate for the size of string you pass in. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Newbie question re vb.net and iterating files
    ... Private Shared Sub CollectBatchFiles(_ ... Private impersonationContext As WindowsImpersonationContext ... Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, ... Public Event eSpecificUserImpersonation(ByVal Success As Boolean) ...
    (microsoft.public.dotnet.languages.vb)