Home All Groups Group Topic Archive Search About

Question on impersonation

Author
11 Apr 2005 8:55 PM
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

Author
12 Apr 2005 1:09 PM
Ken Tucker [MVP]
Hi,

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

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

"Jake Smythe" <rondi***@hotmail.com> wrote in message
news:%23D8xQjtPFHA.1476@TK2MSFTNGP09.phx.gbl...
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
Author
12 Apr 2005 4:36 PM
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

Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%23SMUOD2PFHA.204@TK2MSFTNGP15.phx.gbl...
> Hi,
>
> http://www.dotnet247.com/247reference/msgs/28/144136.aspx
>
> Ken
> --------------
>
> "Jake Smythe" <rondi***@hotmail.com> wrote in message
> news:%23D8xQjtPFHA.1476@TK2MSFTNGP09.phx.gbl...
> 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
>
>
>
Author
12 Apr 2005 9:15 PM
Ken Tucker [MVP]
Hi,

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


Ken
-----------------
"Jake Smythe" <rondi***@hotmail.com> wrote in message
news:O95DE33PFHA.3928@TK2MSFTNGP09.phx.gbl...
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

Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%23SMUOD2PFHA.204@TK2MSFTNGP15.phx.gbl...
> Hi,
>
> http://www.dotnet247.com/247reference/msgs/28/144136.aspx
>
> Ken
> --------------
>
> "Jake Smythe" <rondi***@hotmail.com> wrote in message
> news:%23D8xQjtPFHA.1476@TK2MSFTNGP09.phx.gbl...
> 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
>
>
>