473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Question on impersonation

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
Nov 21 '05 #1
3 1560
Hi,

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

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

"Jake Smythe" <ro******@hotmail.com> wrote in message
news:%2****************@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

Nov 21 '05 #2
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]" <vb***@bellsouth.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Hi,

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

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

"Jake Smythe" <ro******@hotmail.com> wrote in message
news:%2****************@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

Nov 21 '05 #3
Hi,

The Window controller class should get your system to reboot.
http://www.mentalis.org/soft/class.qpx?id=7
Ken
-----------------
"Jake Smythe" <ro******@hotmail.com> wrote in message
news:O9**************@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

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Hi,

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

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

"Jake Smythe" <ro******@hotmail.com> wrote in message
news:%2****************@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


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ripa Horatiu | last post by:
Does anyone knows how can I impersonate to another user (basically Administrator) for a piece of my code? I've tried the samples provided by MS but they didn't worked. -- Horatiu Ripa
12
by: Anil Krishnamurthy | last post by:
We have an ASP.NET application that uses COM objects through Interop. The web application requires access to network and database resources and hence, needs to impersonate a domain account. The...
1
by: techfuzz | last post by:
I'm posting my problem experience and solution I found here for other ASP.NET developers. I have a web application that uses Forms Authentication with Active Directory to control access. In...
0
by: Elliot M. Rodriguez | last post by:
I have a web app that uses Crystal Reports 8.5 along with VB.Net. When attempting to access reports by a UNC file share path, I was getting access denied errors. I solved this problem eventually...
1
by: Peter Johansen | last post by:
Hi, I have a server that I use for shared hosting. For security reasons, I set <identity impersonate="true" /> in my machine.config file, and set allowOverRide="false" to prevent individual webs...
1
by: russell.lane | last post by:
I'm building out a pretty standard n-tier ASP.Net web application. The stack includes application/presentation, biz logic, and data access layers on top of an SQL server back end. We want to...
11
by: Phil | last post by:
Hi, I've currently setup a local user as described in: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnne...
1
by: Patrick | last post by:
I have an ASP.NET web service whose Web.Config is set to use impersonation <authentication mode="Windows" /> <identity impersonate="true" /> Within a Web Method, I want to use...
18
by: =?Utf-8?B?VG9t?= | last post by:
is it possible to add a bunch of users to group and only allow group to access the web page or do I need to add each user to the web.config file? Or is there another way to do this? I just took...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.