Help with Impersonation

From: Jake (rondican@hotmail.com)
Date: 09/26/02


From: "Jake" <rondican@hotmail.com>
Date: Wed, 25 Sep 2002 16:46:01 -0700


Hello,

    We are trying to overload the impersonation method with the sample code
that is in the help file. However each time we try to run the sample class
we get the error
126 Specified module could not be found: 1813

We are trying to impersonate the administrator for this application. Here is
the sample class in the help file. Any help would be most appreciative.

Imports System

Imports System.Runtime.InteropServices

Imports System.Security.Principal

Imports System.Security.Permissions

<Assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum,
UnmanagedCode:=True)>

Public Class Impersonation

<DllImport("C:\\WINNT\\System32\\advapi32.dll")> _

Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal
lpszDomain As String, ByVal lpszPassword As String, _

ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef
phToken As Integer) As Boolean

End Function

<DllImport("C:\\WINNT\\System32\\Kernel32.dll")> _

Public Shared Function GetLastError() As Integer

End Function

Public Shared Sub Main() '(ByVal args() As String)

'The Windows NT user token.

Dim token1 As Integer

'Get the user token for the specified user, machine, and password using the
unmanaged LogonUser method.

'The parameters for LogonUser are the user name, computer name, password,

'Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logon provider
(LOGON32_PROVIDER_DEFAULT),

'and user token.

Dim loggedOn As Boolean = LogonUser("administrator", "HH1", "PASSWORD", 3,
0, token1)

MsgBox("LogonUser called")

'Call GetLastError to try to determine why logon failed if it did not
succeed.

Dim ret As Integer = GetLastError()

MsgBox(ret.ToString)

MsgBox("LogonUser Success? " + loggedOn.ToString)

MsgBox("NT Token Value: " + token1.ToString)

If ret <> 0 Then

MsgBox("Error code (126 == ""Specified module could not be found""): " +
ret.ToString)

End If

'Starting impersonation here:

MsgBox("Before impersonation:")

Dim mWI1 As WindowsIdentity = WindowsIdentity.GetCurrent()

MsgBox(mWI1.Name)

MsgBox(mWI1.Token.ToString)

Dim token2 As IntPtr = New IntPtr(token1)

MsgBox("New identity created:")

Dim mWI2 As WindowsIdentity = New WindowsIdentity(token2)

MsgBox(mWI2.Name)

MsgBox(mWI2.Token.ToString)

'Impersonate the user.

Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()

MsgBox("After impersonation:")

Dim mWI3 As WindowsIdentity = WindowsIdentity.GetCurrent()

MsgBox(mWI3.Name)

MsgBox(mWI3.Token.ToString)

'Revert to previous identity.

mWIC.Undo()

MsgBox("After impersonation is reverted:")

Dim mWI4 As WindowsIdentity = WindowsIdentity.GetCurrent()

MsgBox(mWI4.Name)

MsgBox(mWI4.Token.ToString)

End Sub

End Class



Relevant Pages

  • Re: Impersonation headache
    ... > These both sound like double-hop delegation issues. ... > The impersonation token that WIA creates cannot hop to another machine on ... >> Dim impersonationContext As WindowsImpersonationContext ... >> Dim currentWindowsIdentity As WindowsIdentity ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Impersonation headache
    ... These both sound like double-hop delegation issues. ... impersonation token that WIA creates cannot hop to another machine on the ... > Dim impersonationContext As WindowsImpersonationContext ... > Dim currentWindowsIdentity As WindowsIdentity ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: declarative security and impersonation
    ... all that does is calling IsInRole on Thread.CurrentPrincipal ... you impersonate and AFTER that start a new thread - the impersonation token ... > DirectCast(Thread.CurrentPrincipal.Identity, WindowsIdentity) ... > Dim windowsImpersonationContext As WindowsImpersonationContext = ...
    (microsoft.public.dotnet.security)
  • Re: Impersonation headache
    ... > These both sound like double-hop delegation issues. ... > The impersonation token that WIA creates cannot hop to another machine on ... >> Dim impersonationContext As WindowsImpersonationContext ... >> Dim currentWindowsIdentity As WindowsIdentity ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Prompting for user id/password when using Integrated Security
    ... ' Description: Encapsulates Win32 impersonation API ... (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal ... Private Sub Class_Initialize ... Dim nSuccess As Long ...
    (microsoft.public.data.ado)