PC Review


Reply
Thread Tools Rate Thread

Question on impersonation

 
 
Jake Smythe
Guest
Posts: n/a
 
      11th Apr 2005
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


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      12th Apr 2005
Hi,

http://www.dotnet247.com/247referenc...28/144136.aspx

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

"Jake Smythe" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
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



 
Reply With Quote
 
 
 
 
Jake Smythe
Guest
Posts: n/a
 
      12th Apr 2005
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

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> http://www.dotnet247.com/247referenc...28/144136.aspx
>
> Ken
> --------------
>
> "Jake Smythe" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> 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
>
>
>



 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      12th Apr 2005
Hi,

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


Ken
-----------------
"Jake Smythe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> http://www.dotnet247.com/247referenc...28/144136.aspx
>
> Ken
> --------------
>
> "Jake Smythe" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> 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
>
>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficult security/impersonation question (interact with non-domain systems) Jeff L. Microsoft Dot NET 0 19th Aug 2004 04:29 PM
Identity Impersonation question. Peter Johansen Microsoft ASP .NET 1 2nd May 2004 01:32 PM
Re: Impersonation basic question rpupkin77 Microsoft ASP .NET 0 23rd Mar 2004 08:20 PM
Impersonation question Ryan Microsoft ASP .NET 0 5th Jan 2004 05:30 PM
Impersonation Question - Encrypting Passwords Elliot M. Rodriguez Microsoft ASP .NET 0 4th Nov 2003 09:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:11 PM.