使用备用凭据管理远程服务

时间:2010-07-19 16:25:15

标签: vb.net windows-services credentials

- 原帖 -

我正在尝试使用备用凭据管理(启动/停止)远程计算机上的Windows服务。我知道我可以使用ServiceController类来使用我当前的凭据来管理服务:

Dim sc As New ServiceController(ServiceName, ComputerName)

但我想使用不同的凭据。我正在使用的其他类(DirectoryEntry和System.Management)都支持使用备用凭据...非常感谢帮助。

- 工作代码(根据已接受的答案建立) -

我必须承认我怀疑它会起作用......但下面是代码。我不得不对你建议的代码做一个小改动。每当我尝试IPC $时,它会返回53结果代码,即使我确定该共享存在。所以在另一个网站的建议下,我删除了共享,只删除了计算机名称,这很有效。

Imports System.Runtime.InteropServices
Imports System.Net
Imports System.IO
Imports System.ServiceProcess

Module Module1

    Sub Main()
        Dim Computername As String = "SomeComputer"
        'Create connection to remote computer'
        Using nc As New NetworkConnection("\\" + Computername, New NetworkCredential("Domain\User", "Password"))
            Dim sc As New ServiceController("Windows Firewall/Internet Connection Sharing (ICS)", Computername)
            'now we can start/stop/whatever we want here'
        End Using
        Console.ReadLine()
    End Sub

    Public Class NetworkConnection
        Implements IDisposable


        Private _networkName As String

        Public Sub New(ByVal networkName As String, ByVal credentials As NetworkCredential)
            _networkName = networkName

            Dim netResource = New NetResource() With { _
             .Scope = ResourceScope.GlobalNetwork, _
             .ResourceType = ResourceType.Disk, _
             .DisplayType = ResourceDisplaytype.Share, _
             .RemoteName = networkName _
            }

            Dim result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0)

            If result <> 0 Then
                Throw New IOException("Error connecting to remote share", result)
            End If
        End Sub

        Protected Overrides Sub Finalize()
            Try
                Dispose(False)
            Finally
                MyBase.Finalize()
            End Try
        End Sub

        Public Sub Dispose() Implements System.IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub

        Protected Sub Dispose(ByVal disposing As Boolean)
            WNetCancelConnection2(_networkName, 0, True)
        End Sub

        <DllImport("mpr.dll")> _
        Private Shared Function WNetAddConnection2(ByVal netResource As NetResource, ByVal password As String, ByVal username As String, ByVal flags As Integer) As Integer
        End Function

        <DllImport("mpr.dll")> _
        Private Shared Function WNetCancelConnection2(ByVal name As String, ByVal flags As Integer, ByVal force As Boolean) As Integer
        End Function
    End Class

    <StructLayout(LayoutKind.Sequential)> _
    Public Class NetResource
        Public Scope As ResourceScope
        Public ResourceType As ResourceType
        Public DisplayType As ResourceDisplaytype
        Public Usage As Integer
        Public LocalName As String
        Public RemoteName As String
        Public Comment As String
        Public Provider As String
    End Class

    Public Enum ResourceScope As Integer
        Connected = 1
        GlobalNetwork
        Remembered
        Recent
        Context
    End Enum

    Public Enum ResourceType As Integer
        Any = 0
        Disk = 1
        Print = 2
        Reserved = 8
    End Enum

    Public Enum ResourceDisplaytype As Integer
        Generic = &H0
        Domain = &H1
        Server = &H2
        Share = &H3
        File = &H4
        Group = &H5
        Network = &H6
        Root = &H7
        Shareadmin = &H8
        Directory = &H9
        Tree = &HA
        Ndscontainer = &HB
    End Enum
End Module

1 个答案:

答案 0 :(得分:2)

要进行远程登录,您应使用WNetAddConnection2(请参阅http://msdn.microsoft.com/en-us/library/aa385413.aspx)或NetUseAdd(请参阅http://msdn.microsoft.com/en-us/library/aa370645.aspx)API。您可以使用\\RemoteComputer\IPC$作为目标资源。

更新基于评论中的问题:关于IPC $会话的解释可能很长。只是主要信息。

如果您想在远程计算机上执行某些操作,首先要做的是与远程计算机建立经过身份验证的“连接”。远程计算机上的网络登录远程登录)将完成,这与其他本地登录相同。网络登录会话保持不变,如果您与例如\\RemoteComputer\share1和计算机上的另一个程序有连接,请尝试访问\\RemoteComputer\share2,将使用相同的会话。

您可以使用net.exe模拟情况。只需启动cmd.exe并输入

即可
net use \\RemoteComputer\IPC$ /u:Domain\User password

net use \\RemoteComputer\IPC$ /u:RemoteComputer\LocalRemoteUser password

然后您将与目标计算机建立连接。然后,您可以在资源管理器中键入\\RemoteComputer\AnyShare,并在用户的Domain\UserRemoteComputer\LocalRemoteUser凭据下访问文件系统。断开连接

net use \\RemoteComputer\IPC /d

如果您尝试在远程计算机上启动/停止服务,则将尝试建立相同的IPC会话。如果您已经使用其中一个用户凭据进行此类会话,则将使用该会话。函数WNetAddConnection2NetUseAdd可用作“净使用”的替代。如果您永久要使用其他用户的凭据访问远程计算机,则可以使用CredWriteCredWriteDomainCredentialsCredUIPromptForCredentials / CredUIPromptForWindowsCredentials。 Cred-function对我来说似乎不是最好的方式。