Impersonation-Security ADVAPI32.DLL Problem

Im coding the security component for the current RP we are developing in my company, the main problem is that when I use the LogonUser function from this DLL, I can only validate ADMIN users.. i.e. I create the John.Doe user, in the LOCALMACHINE (AdminTools->UserConfig->LocalUsers) and then added it to the Users group. Try the mentioned function in a simple program and it fails... return to the control panel, changed the the permisions of John.Doe to Admin, and the program just seems to work fine... so I supposed that only works with the Admin user or Im missing anything
Below is the code for the program and the CLASS USERMANAGER wich has the mentioned LOGONUSER function.


Luis.


Try
            Dim tokenHandle As New IntPtr(0)
            Dim ret As Integer
            '**********************
            Const LOGON32_LOGON_INTERACTIVE As Long = 2
            Const LOGON32_PROVIDER_DEFAULT As Integer = 0

            '************
            tokenHandle = IntPtr.Zero
            Dim oUSer As New UserManager
            Dim domain As String = System.Environment.MachineName
            Dim user As String = txtlogin.Text
            Dim pass As String = txtpass.Text
            If oUSer.LogonUser(user, domain, pass, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle) Then
                lblMensaje.Text = "ENTRO"
            Else
                lblMensaje.Text = "NO ENTRO"
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Informacion", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
'***********************
'***********************
Imports System.Security.Principal
Imports System.Runtime.InteropServices
Public Class UserManager
    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 CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean

    Public Shared Sub CreateIdentity(ByVal User As String, ByVal Domain As String, ByVal Password As String)
        Try
            Dim tokenHandle As New IntPtr(0)
            Dim ret As Integer
            Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
            Dim LOGON32_LOGON_NETWORK_CLEARTEXT As Integer = 3
            tokenHandle = IntPtr.Zero
            Dim returnValue As Boolean = LogonUser(User, Domain, Password, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, tokenHandle)
            If False = returnValue Then
                ret = Marshal.GetLastWin32Error()
                Throw New Exception("LogonUser failed with error code: " + ret)
            End If
            Dim id As New WindowsIdentity(tokenHandle)
            CloseHandle(tokenHandle)
            id.Impersonate()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class



Answer this question

Impersonation-Security ADVAPI32.DLL Problem

  • Anddos

    What OS are you running on
  • matt love

    Luis,

    What errors do you get and when do you get the errors

  • saspsnvg111381

    I have windows XP Professional. Can anyone try this code with a non-admin user and the same user but with admin priv. remember the user MUST be created in the local machine, NOT in the domain... so ControlPanel -> AdminTools-> Computer Management -> Local Users and Groups
    Thanks.

    Luis.

  • Impersonation-Security ADVAPI32.DLL Problem