Start/Stop service from an ASP . NET page
Asked Answered
I

2

4

I implemented a web page (ASP .NET, VB) to start/stop a Windows service.

I used impersonification, as described here: http://support.microsoft.com/kb/306158

Everything it's ok when the page reads the service's status:

_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

The problem occurs when the page tries to start (or stop) the service:

_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

Comments:

  • Exception is "Access denied", but the impersonated user is the same, both for the status and the start/stop
  • The user is the same user I logged in the PC. And I can start/stop user from the Service console

Any idea?

I added the identity element in the web.config and it works, but I don't want the whole application impersonates the Administrator user (it was just a test):

<identity impersonate="true" userName="domain\user" password="password"/>
Incautious answered 15/10, 2012 at 10:30 Comment(1)
It looks impersonification is not needed to read Service's status. That's why it works :-(Incautious
I
2

Solution proposed here http://support.microsoft.com/kb/306158 said

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

It didn't work for me.

I replaced LOGON_32_LOGON_INTERACTIVE with LOGON32_LOGON_SERVICE:

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

And it works for me: the ASP .NET page can start/stop the service.

Incautious answered 15/10, 2012 at 13:55 Comment(0)
E
1

In my case, both LOGON32_LOGON_INTERACTIVE and LOGON32_LOGON_SERVICE don't work.

However, LOGON32_LOGON_NETWORK works for me. Note that the user has admin privilege therefore is able to start/stop the windows service.

Exclamatory answered 13/2, 2015 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.