Question on impersonation

J

Jake Smythe

Hello,

I have some code that impersonates a user upon launching of the
application. We now have the need to run some command line items. The
impersonation doesn't seem to pass to the commands being run. Is there a way
to do this? Basically looking for a way do a runas on a command line through
an application. Thanks in advance. Below is some sample code, where we need
to impersonate an admin to run command line code.

Private Sub test
ImpersonateAdmin("adminpass")
ForceReboot(60)
End Sub


Private Sub ImpersonateAdmin(byval strPass as string)
Dim token1 As Integer
Dim clsImpersonate As New ImpersonationXP
Dim loggedOn As Boolean = clsImpersonate.LogonUser("administrator",
".", strPass, 3, 0, token1)
Dim ret As Integer = clsImpersonate.GetLastError()
mWI1 = WindowsIdentity.GetCurrent()
Dim token2 As IntPtr = New IntPtr(token1)
If token2.ToInt32 = 0 Then
Throw New Exception("Error during impersonation. Please contact
support")
End If
mWI2 = New WindowsIdentity(token2)
mWIC = mWI2.Impersonate()
End Sub


Private Function ForceReboot(ByVal intTime As Integer) As Boolean
Dim psi As New System.Diagnostics.ProcessStartInfo
psi.FileName = "shutdown.exe"
psi.Arguments = " -r -f -t " & intTime & " -d up:125:1"
psi.UseShellExecute = False
psi.RedirectStandardOutput = True
' psi.CreateNoWindow = True
psi.WorkingDirectory = "c:\"
'psi.CreateNoWindow = True
Dim p As Process
Dim cmdOutput As String
p = Process.Start(psi)
Try
cmdOutput = p.StandardOutput.ReadToEnd()
p.WaitForExit()
Return True
Finally
If Not p.HasExited Then
p.Kill()
End If
End Try
Return False
End Function
 
K

Ken Tucker [MVP]

Hi,

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

Ken
--------------

Hello,

I have some code that impersonates a user upon launching of the
application. We now have the need to run some command line items. The
impersonation doesn't seem to pass to the commands being run. Is there a way
to do this? Basically looking for a way do a runas on a command line through
an application. Thanks in advance. Below is some sample code, where we need
to impersonate an admin to run command line code.

Private Sub test
ImpersonateAdmin("adminpass")
ForceReboot(60)
End Sub


Private Sub ImpersonateAdmin(byval strPass as string)
Dim token1 As Integer
Dim clsImpersonate As New ImpersonationXP
Dim loggedOn As Boolean = clsImpersonate.LogonUser("administrator",
".", strPass, 3, 0, token1)
Dim ret As Integer = clsImpersonate.GetLastError()
mWI1 = WindowsIdentity.GetCurrent()
Dim token2 As IntPtr = New IntPtr(token1)
If token2.ToInt32 = 0 Then
Throw New Exception("Error during impersonation. Please contact
support")
End If
mWI2 = New WindowsIdentity(token2)
mWIC = mWI2.Impersonate()
End Sub


Private Function ForceReboot(ByVal intTime As Integer) As Boolean
Dim psi As New System.Diagnostics.ProcessStartInfo
psi.FileName = "shutdown.exe"
psi.Arguments = " -r -f -t " & intTime & " -d up:125:1"
psi.UseShellExecute = False
psi.RedirectStandardOutput = True
' psi.CreateNoWindow = True
psi.WorkingDirectory = "c:\"
'psi.CreateNoWindow = True
Dim p As Process
Dim cmdOutput As String
p = Process.Start(psi)
Try
cmdOutput = p.StandardOutput.ReadToEnd()
p.WaitForExit()
Return True
Finally
If Not p.HasExited Then
p.Kill()
End If
End Try
Return False
End Function
 
J

Jake Smythe

Ken,

I have that below in my code the problem is that after impersonation I
call the ForceReboot() function which runs the command line. This will not
execute. It seems that the impersonation is not being ran under
impersonation. Thoughts?

Jake
 
K

Ken Tucker [MVP]

Hi,

The Window controller class should get your system to reboot.
http://www.mentalis.org/soft/class.qpx?id=7


Ken
-----------------
Ken,

I have that below in my code the problem is that after impersonation I
call the ForceReboot() function which runs the command line. This will not
execute. It seems that the impersonation is not being ran under
impersonation. Thoughts?

Jake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top