Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Impersonation and start a process

Status
Not open for further replies.

mrdance

Programmer
Joined
Apr 17, 2001
Messages
308
Location
SE
Since XP SP2 and Windows 2003 you can't use CreateProcessWithLogonW under the system account in a Windows Service. Does anyone have a working sample of using the LogonUser and CreateProcessAsUser together or any other example that works.

thanks / Henrik

--- neteject.com - Internet Solutions ---
 
this is how I use impersination

Code:
Imports System.Security.Principal
Imports System.Security.Permissions

 Private 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("Kernel32.dll")> _
Private Shared Function GetLastError() As Integer
        End Function

        <SecurityPermissionAttribute(SecurityAction.Demand, _
        ControlPrincipal:=True, UnmanagedCode:=True)> _
        Private Shared Function GetWindowsIdentity(ByVal UserName As String, _
        ByVal Domain As String, ByVal Password As String) As WindowsIdentity
            Dim SecurityToken As Integer
            Dim Success As Boolean
            Success = LogonUser(UserName, Domain, Password, Logon.NetworkCleartext, Provider.Windows2000, SecurityToken)
            If Not Success Then
                Throw New System.Exception("Logon Failed. Error: " & _
                  GetLastError())
            End If
            GetWindowsIdentity = New WindowsIdentity(New _
                                                     IntPtr(SecurityToken))
        End Function

        Public Shared Sub ImpersonateAdministrator(ByVal Impersonate As Boolean)
            Dim newidentity As WindowsIdentity
            Dim oldidentity As WindowsIdentity
            Dim Username, Domain, Password As String

            If Impersonate = True Then
                newidentity = GetWindowsIdentity("user", "domainname", "password")
                NewContext = newidentity.Impersonate
            Else
                NewContext.Undo()
            End If
        End Sub


Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Chrissie, help!

I am getting these errors when pasting your code:

at DllImport: not defined
at NewContext: not declared

What am I missing?
 
LogonUser is ok. But I want it to work with CreateProcessAsUser.



--- neteject.com - Internet Solutions ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top