4

我实现了一个网页(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

评论:

  • 例外是“拒绝访问”,但模拟用户是相同的,无论是状态还是启动/停止
  • 该用户与我登录PC的用户相同。我可以从服务控制台启动/停止用户

任何的想法?

我在 web.config 中添加了标识元素并且它可以工作,但我不希望整个应用程序模拟管理员用户(这只是一个测试):

<identity impersonate="true" userName="domain\user" password="password"/>
4

2 回答 2

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 页面可以启动/停止服务。

于 2012-10-15T13:55:24.687 回答
1

就我而言,LOGON32_LOGON_INTERACTIVE 和 LOGON32_LOGON_SERVICE 都不起作用。

但是, LOGON32_LOGON_NETWORK 对我有用。请注意,用户具有管理员权限,因此能够启动/停止 Windows 服务。

于 2015-02-13T16:17:52.860 回答