我实现了一个网页(ASP .NET,VB)来启动/停止Windows服务。
我使用了模仿,如下所述:http://support.microsoft.com/kb/306158
当页面读取服务的状态时,一切正常:
_domain = Me.TextBoxDomain.Text
_user = Me.TextBoxUserName.Text
_password = Me.TextBoxPassword.Text
_s = New ServiceController(Constant.ServiceName)
If impersonateValidUser(_user, _domain, _password) Then
Me.LabelServerStatusValue.Text = _s.Status.ToString
undoImpersonation()
Else
'Error
End If
当页面尝试启动(或停止)服务时会出现问题:
_domain = Me.TextBoxDomain.Text
_user = Me.TextBoxUserName.Text
_password = Me.TextBoxPassword.Text
_s = New ServiceController(Constant.ServiceName)
If impersonateValidUser(_user, _domain, _password) Then
If _s.Status = ServiceControllerStatus.Stopped And _s.Status <> ServiceControllerStatus.StartPending Then
_s.Start()
_s.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(5))
End If
undoImpersonation()
Else
'Error
End If
评论:
有什么想法吗?
我在web.config中添加了identity元素并且它可以工作,但我不希望整个应用程序冒充管理员用户(这只是一个测试):
<identity impersonate="true" userName="domain\user" password="password"/>
答案 0 :(得分:2)
此处提出的解决方案http://support.microsoft.com/kb/306158 所述
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
[...]
End if
它对我不起作用。
我用LOGON32_LOGON_SERVICE替换了LOGON_32_LOGON_INTERACTIVE:
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_LOGON_NETWORK As Integer = 3
Dim LOGON32_LOGON_BATCH As Integer = 4
Dim LOGON32_LOGON_SERVICE As Integer = 5
Dim LOGON32_LOGON_UNLOCK As Integer = 7
Dim LOGON32_LOGON_NETWORK_CLEARTEXT As Integer = 8
Dim LOGON32_LOGON_NEW_CREDENTIALS As Integer = 9
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
If LogonUserA(userName, domain, password, LOGON32_LOGON_SERVICE,LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
[...]
End if
它适用于我:ASP .NET页面可以启动/停止服务。
答案 1 :(得分:1)
就我而言,LOGON32_LOGON_INTERACTIVE和LOGON32_LOGON_SERVICE都无法正常工作。
但是,LOGON32_LOGON_NETWORK适合我。请注意,用户具有管理员权限,因此能够启动/停止Windows服务。