Authenticating WMI Requests


Your code often will make WMI queries to remote servers. In most of these cases, you'll have to authenticate to the remote servers. This section talks about doing that.

The biggest difference between the code you're going to look at now and the examples you've already seen is that this next example uses a ManagementObject constructor that takes an additional argument in its constructor. The constructor used takes a ManagementScope object and a ManagementPath object. The examples in the previous section only specified a ManagementPath .

The ManagementScope object is used to specify information with regard to the remote server. The code in Listing 19.3 shows how to create a ManagementScope object. The constructor used in Listing 19.3 takes a server path string and a ConnectionOptions object as arguments. The server path string obviously specifies the path to the server. The ConnectionOptions object contains information that allows user credentials to be presented for authentication.

Listing 19.3 Connecting to a Remote Server and Providing Authentication Credentials
 ConnectionOptions options = new ConnectionOptions();  options.Username = "domain\username";  options.Password = "password";  ManagementScope scope = new ManagementScope( "\\servername\root\cimv2",      options );  try  {      scope.Connect();      ManagementObject disk = new ManagementObject( scope,          new ManagementPath( "Win32_logicaldisk='c:'" ), null );      disk.Get();  }  catch( Exception ex )  {      // Handle exception here.   } 

Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233 © 2008-2017.
If you may any questions please contact us: