我在VB.net中编写的程序存在问题。它是一个在远程服务器上启动第三方服务的程序。当我运行该程序它工作得很好,但我有管理员权限。这个想法是,当某个服务停止时,某些人可以使用这个程序。
我使用模拟通过使用以下代码授予程序管理员权限:
Public Sub ImpersonateStart(ByVal Domain As String, ByVal userName As
String, ByVal Password As String)
Try
tokenHandle = IntPtr.Zero
' Call LogonUser to obtain a handle to an access token.
Dim returnValue As Boolean = LogonUser(userName, Domain, Password,
2, 0, tokenHandle)
'check if logon successful
If returnValue = False Then
Dim ret As Integer = Marshal.GetLastWin32Error()
Console.WriteLine("LogonUser failed with error code : {0}", ret)
Throw New System.ComponentModel.Win32Exception(ret)
Exit Sub
End If
'Logon succeeded
' Use the token handle returned by LogonUser.
Dim newId As New WindowsIdentity(tokenHandle)
impersonatedUser = newId.Impersonate()
Catch ex As Exception
Throw ex
Exit Sub
End Try
End Sub
在我使用的实际代码中 imp.ImpersonateStart(" DOMAIN","管理员","密码")
日Thnx