ConnectionOptions.Impersonation 屬性

定義

取得或設定 COM 模擬等級,以使用於這個連接中的作業。

C#
public System.Management.ImpersonationLevel Impersonation { get; set; }

屬性值

傳回 ImpersonationLevel 列舉值,表示用於連接 WMI 的模擬等級。

範例

下列範例會連線到遠端電腦,並顯示遠端電腦上作業系統的相關信息。 會建立 , ConnectionOptions 以使用所需的連線選項連線到遠端電腦。

C#
using System;
using System.Management;
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.
        // This example uses the default values.
        ConnectionOptions options =
            new ConnectionOptions();
        options.Impersonation =
            System.Management.ImpersonationLevel.Impersonate;

        // 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"]);
        }
    }
}

備註

當提供者是受信任的應用程式或服務時,此 ImpersonationLevel.Impersonate 設定會很有利。 它不需要提供者執行要求的作業的用戶端身分識別和存取檢查。 不過,如果基於某些原因,提供者無法信任,允許它模擬用戶端可能會構成安全性威脅。 在這種情況下,我們建議用戶端將此屬性設定為較低的值,例如 ImpersonationLevel.Identify。 請注意,這可能會導致提供者無法執行要求的作業,因為沒有足夠的許可權或無法執行存取檢查。

屬性值

要用於此連線中作業的 COM 模擬層級。 預設值為 ImpersonationLevel.Impersonate,表示 WMI 提供者可以在此連接中執行要求的作業時模擬用戶端。

.NET Framework 安全性

完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1