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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run bit of code under dif impersonation

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN

Firs for you want to know why, I need outlook 2000 automation so the code needs to run under a specific user:

Here is the code like I made it in a winform app:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If impersonateValidUser("username", "domain", "password") Then
MsgBox("running as harmpie")
undoImpersonation()
Else
MsgBox("still not working.")
End If
End Sub



' this bit is needed to run code as another user (impersonate code)
' only works if the current account that the code is running on
' and the account that you want to run the code under
' have "Act as part of the operating system" privilege
' you can do so by:
'control panel -> administrative tools -> Local Security Policy ->
'local policies -> user rights assignment Grant the
'"Act as part of the operating system" privilege to the ASPNET account.

Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0

Dim impersonationContext As System.Security.Principal.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 System.Security.Principal.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 System.Security.Principal.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
Private Sub undoImpersonation()
impersonationContext.Undo()
End Sub
' end of the impersonate code


When Everyone, computername\ASPNET and domain\username can &quot;Act as part of the operating system&quot; it still won't work.

My question is simple:
Why?



Greetings, Harm Meijer
 
I got it working in my asp.net application.

Found out what what wrong, the account that I used was on another domain (huh).





Greetings, Harm Meijer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top