Re: Impersonation half way working

From: Ken Tucker [MVP] (vb2ae_at_bellsouth.net)
Date: 02/24/04


Date: Tue, 24 Feb 2004 01:29:37 -0500

Hi,

http://www.dotnet247.com/247reference/msgs/28/144136.aspx

Ken
------------------
"William Oliveri" <wuji@bigvalley.net> wrote in message
news:ORBoDcl%23DHA.2824@tk2msftngp13.phx.gbl...
> Hello all,
>
> I have a working example of Impersonation where I receive a token and a
true
> response for a specific user. However, if I try to access a drive that
has
> only that user's permission to access I get denied. Code below.
>
> I tried this on a XP box and a W2K box with the same results. This
> impersonation is in a Active Directory environment with W2K Server.
>
> Thanks for any help
>
> Bill
> Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
> Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
> Dim impersonationContext As WindowsImpersonationContext
> Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As
> String, _
> ByVal lpszDomain As String, _
> ByVal lpszPassword As String, _
> ByVal dwLogonType As Integer, _
> ByVal dwLogonProvider As Integer, _
> ByRef phToken As IntPtr) As Integer
>
> Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal
> ExistingTokenHandle As IntPtr, ByVal ImpersonationLevel As Integer, ByRef
> DuplicateTokenHandle As IntPtr) As Integer
>
> Private Function impersonateValidUser(ByVal userName As String, _
> ByVal domain As String, ByVal password As String) As Boolean
>
> Dim tempWindowsIdentity As WindowsIdentity
> Dim token As IntPtr
> Dim tokenDuplicate As IntPtr
> If LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, _
> LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
> If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
> tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
> impersonationContext = tempWindowsIdentity.Impersonate()
> If impersonationContext Is Nothing Then
> impersonateValidUser = False
> Else
> impersonateValidUser = True
> End If
> Else
> impersonateValidUser = False
> End If
> Else
> impersonateValidUser = False
> End If
> End Function
>
>



Relevant Pages

  • logonuser api returns 127
    ... Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As ... String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... Dim LogonProvider As Integer ... impersonateValidUser = False ...
    (microsoft.public.dotnet.languages.vb)
  • Re: logonuser api returns 127
    ... > String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... > Dim LogonProvider As Integer ... > impersonateValidUser = False ... are you absolutely sure that GetLastError is returning ...
    (microsoft.public.dotnet.languages.vb)
  • problems with Impersonate
    ... String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... Declare Auto Function RevertToSelf Lib "advapi32.dll" As Integer ... Sub Main ...
    (microsoft.public.dotnet.languages.vb)
  • Re: problems with Impersonate
    ... String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal ... Declare Auto Function RevertToSelf Lib "advapi32.dll" As Integer ... Sub Main ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Impersonate via a remote workgroup
    ... Dim tempWindowsIdentity As WindowsIdentity ... Dim tokenDuplicate As IntPtr = IntPtr.Zero ... impersonateValidUser = False ...
    (microsoft.public.dotnet.framework.aspnet.security)