ConnectionOptions.Impersonation 属性

定义

获取或设置用于此连接中的操作的 COM 模拟级别。

public:
 property System::Management::ImpersonationLevel Impersonation { System::Management::ImpersonationLevel get(); void set(System::Management::ImpersonationLevel value); };
public System.Management.ImpersonationLevel Impersonation { get; set; }
member this.Impersonation : System.Management.ImpersonationLevel with get, set
Public Property Impersonation As ImpersonationLevel

属性值

返回 ImpersonationLevel 枚举值,该值指示用于连接到 WMI 的模拟级别。

示例

以下示例连接到远程计算机,并显示有关远程计算机上的操作系统的信息。 ConnectionOptions创建 以使用所需连接选项连接到远程计算机。

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"]);
        }
    }
}
Imports System.Management
Public Class RemoteConnect

    Public Overloads Shared Function Main( _
    ByVal args() As String) As Integer

        ' 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
        Dim options As ConnectionOptions
        options = New ConnectionOptions
        options.Impersonation = 3
        ' System.Management.ImpersonationLevel.Impersonate = 3

        ' 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.
        Dim scope As ManagementScope
        scope = New ManagementScope( _
            "\\FullComputerName\root\cimv2", options)
        scope.Connect()

        ' Query system for Operating System information
        Dim query As ObjectQuery
        query = New ObjectQuery( _
            "SELECT * FROM Win32_OperatingSystem")
        Dim searcher As ManagementObjectSearcher
        searcher = _
            New ManagementObjectSearcher(scope, query)

        Dim queryCollection As ManagementObjectCollection
        queryCollection = searcher.Get()

        Dim m As ManagementObject
        For Each 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"))
        Next

        Return 0
    End Function
End Class

注解

当提供程序是受信任的应用程序或服务时,设置 ImpersonationLevel.Impersonate 是有利的。 它使提供程序无需对请求的操作执行客户端标识和访问检查。 但是,如果出于某种原因无法信任提供程序,则允许它模拟客户端可能会构成安全威胁。 在这种情况下,建议客户端将此属性设置为较小的值,例如 ImpersonationLevel.Identify。 请注意,这可能会导致提供程序无法执行请求的操作,因为缺少足够的权限或无法执行访问检查。

属性值

要用于此连接中的操作的 COM 模拟级别。 默认值 ImpersonationLevel.Impersonate为 ,指示 WMI 提供程序可以在此连接中执行请求的操作时模拟客户端。

.NET Framework 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于