How can you remotely log in to a remote computer using C #?

I am trying to write a small command line application with C # that will prompt you to enter a username and password that will be used to log in to several remote computers that are on the same network / domain and start a local session.

I tried to connect to a remote computer and request information about the operating system of the remote PC using the following code:

ConnectionOptions options = new ConnectionOptions();

ManagementScope scope = new ManagementScope(@"\\REMOTE_COMPUTER_NAME");
scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();

foreach (ManagementObject m in queryCollection)
{
    // Display the remote computer information
    Console.WriteLine("Computer Name : {0}", m["csname"]);
    Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
    Console.WriteLine("Operating System: {0}", m["Caption"]);
    Console.WriteLine("Version: {0}", m["Version"]);
    Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
}

However, this only returns information on the local PC I'm working on, and not on the remote PC.

I do not notice something with this code? And is there a suitable approach to achieve what I'm trying to do?

+4
source share
2 answers

, , . advapi32.logonuser

:

[DllImport("advapi32.dll")]
public static extern bool LogonUser(string name, string domain, string pass, 
int logType, int logpv, out IntPtr pht);

IntPtr ptr;
// LogonUser("username", "remotemachine", "password", 2, 0, out ptr);
LogonUser("username", "remotemachine", "password", 9, 0, out ptr);
WindowsIdentity windowsIdentity = new WindowsIdentity(ptr);
var impersonationContext = windowsIdentity.Impersonate();

// your code goes here...

impersonationContext.Undo();

. , . . LOGON32_PROVIDER_WINNT50. . Windows NT: .

http://www.pinvoke.net/default.aspx/advapi32.logonuser

cassia

ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetRemoteServer("servername"))
{
    server.Open();
    foreach (ITerminalServicesSession session in server.GetSessions())
    { 
        Console.WriteLine("Hi there, " + session.UserAccount + " on session " + session.SessionId);
        Console.WriteLine("It looks like you logged on at " + session.LoginTime +
                            " and are now " + session.ConnectionState);
    }
}
+1

ConnectionOptions ManagementScope

    public void GetSystemInformation(string _yourDomain, string _hostName, string _userName, SecureString _password)
    {
        ManagementScope Scope = null;
        string computerName = _hostName;
        string userName = _userName;
        SecureString password = _password;
        ManagementObjectCollection collection = null;

        try
        {
            SelectQuery query = new SelectQuery("SELECT * FROM Win32_OperatingSystem");
            //string query = "SELECT * FROM Win32_NetworkAdapterConfiguration" + " WHERE IPEnabled = 'TRUE'";

            var options = new ConnectionOptions
            {
                  EnablePrivileges = false,
                  Impersonation = ImpersonationLevel.Impersonate,
                  Username = _userName,
                  SecurePassword = _password,
                  Authority = "ntlmdomain:" + _yourDomain
            };
            Scope.Options = options;
            Scope.Connect();

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(Scope, query);
            collection = searcher.Get();

           //Do something with the collection
        }
        catch (ManagementException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (UnauthorizedAccessException ex)
        {
            throw new ArgumentException(ex.Message);
        }
    }

    private static SecureString CreateSecuredString(string pw)
    {
        var secureString = new SecureString();

        foreach (var c in pw)
        {
            secureString.AppendChar(c);
        }

        return secureString;
    }

, EnablePrivileges Impersonation

(), .

0

All Articles