Export (0) Print
Expand All

ConnectionOptions Constructor (String, String, String, String, ImpersonationLevel, AuthenticationLevel, Boolean, ManagementNamedValueCollection, TimeSpan)

Initializes a new instance of the ConnectionOptions class to be used for a WMI connection, using the specified values.

Namespace:  System.Management
Assembly:  System.Management (in System.Management.dll)

public ConnectionOptions(
	string locale,
	string username,
	string password,
	string authority,
	ImpersonationLevel impersonation,
	AuthenticationLevel authentication,
	bool enablePrivileges,
	ManagementNamedValueCollection context,
	TimeSpan timeout
)

Parameters

locale
Type: System.String

The locale to be used for the connection.

username
Type: System.String

The user name to be used for the connection. If null, the credentials of the currently logged-on user are used.

password
Type: System.String

The password for the given user name. If the user name is also null, the credentials used will be those of the currently logged-on user.

authority
Type: System.String

The authority to be used to authenticate the specified user.

impersonation
Type: System.Management.ImpersonationLevel

The COM impersonation level to be used for the connection.

authentication
Type: System.Management.AuthenticationLevel

The COM authentication level to be used for the connection.

enablePrivileges
Type: System.Boolean

true to enable special user privileges; otherwise, false. This parameter should only be used when performing an operation that requires special Windows NT user privileges.

context
Type: System.Management.ManagementNamedValueCollection

A provider-specific, named value pairs object to be passed through to the provider.

timeout
Type: System.TimeSpan

Reserved for future use.

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

The following example connects to a remote computer and displays information about the operating system on the remote computer. A ConnectionOptions is created to connect to the remote computer with the desired connection options.

using System;
using System.Management;
using System.Security;

public class RemoteConnect
{
    public static void Main()
    {
        // Build an options object for the remote connection 
        // if you plan to connect to the remote 
        // computer with a different user name 
        // and password than the one you are currently using.
        SecureString pw = GetPassword();
        ConnectionOptions options =
            new ConnectionOptions("MS_409", "userName", pw,
            "ntlmdomain:DOMAIN",
            System.Management.ImpersonationLevel.Impersonate,
            System.Management.AuthenticationLevel.Default, true,
            null, System.TimeSpan.MaxValue);
        pw.Dispose();

        // Make a connection to a remote computer. 
        // Replace the "FullComputerName" section of the
        // string "\\\\FullComputerName\\root\\cimv2" with
        // the full computer name or IP address of the 
        // remote computer.
        ManagementScope scope =
            new ManagementScope(
            "\\\\FullComputerName\\root\\cimv2", options);
        scope.Connect();

        //Query system for Operating System information
        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"]);
        }
    }

    /// <summary> 
    /// Read a password from the console into a SecureString 
    /// </summary> 
    /// <returns>Password stored in a secure string</returns> 
    public static SecureString GetPassword()
    {
        SecureString password = new SecureString();
        Console.WriteLine("Enter password: ");

        // get the first character of the password
        ConsoleKeyInfo nextKey = Console.ReadKey(true);

        while (nextKey.Key != ConsoleKey.Enter)
        {
            if (nextKey.Key == ConsoleKey.Backspace)
            {
                if (password.Length > 0)
                {
                    password.RemoveAt(password.Length - 1);

                    // erase the last * as well
                    Console.Write(nextKey.KeyChar);
                    Console.Write(" ");
                    Console.Write(nextKey.KeyChar);
                }
            }
            else
            {
                password.AppendChar(nextKey.KeyChar);
                Console.Write("*");
            }

            nextKey = Console.ReadKey(true);
        }

        Console.WriteLine();

        // lock the password down
        password.MakeReadOnly();
        return password;
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft