How to Connect to a System Center Data Access Service by Using Specified User Credentials

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

An Operations Manager software development kit (SDK) client can connect to the System Center Data Access service by using a given set of user credentials.

noteNote
The account that is used for authentication must be included in an Operations Manager user-role profile. For more information, see How to Connect an Operations Manager SDK Client to the System Center Data Access Service.

Example

The following example demonstrates how to connect to the System Center Data Access service by using a provided user name, domain, and password. The example also shows how to handle some common connection failures.

/// <summary>
/// Prompts for a username, domain, and password, and connects to the 
/// SDK Service by using the supplied credentials.
/// </summary>
using System;
using System.Collections.Generic;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using System.Security;
using System.ServiceProcess;
using System.Text;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			// Define the server that you want to connect to.
			string serverName = "serverName";
		
			Console.WriteLine("Please specify the credentials to use when connecting to the SDK Service.");
			// Get name.
			Console.Write("User name: ");
			string name = Console.ReadLine();

			// Get domain.
			Console.Write("Domain: ");
			string userDomain = Console.ReadLine();

			// Get password.
			SecureString password = new SecureString();
			Console.Write("Password: ");
			while (true)
			{
				// Display asterisks for entered characters.
				ConsoleKeyInfo cki = Console.ReadKey(true);

				// If password is complete, connect with supplied credentials.
				if (cki.Key == ConsoleKey.Enter)
				{
					Console.Write(Environment.NewLine);
					break;
			}
				else if (cki.Key == ConsoleKey.Backspace)
				{
					// Remove the last asterisk from the console.
					if (password.Length > 0)
					{
						Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
						Console.Write(" ");
						Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
						password.RemoveAt(password.Length - 1);
				}
			}
				else
				{
					password.AppendChar(cki.KeyChar);
					Console.Write("*");
			}
		}


			try
			{
				ManagementGroupConnectionSettings mgSettings =
					new ManagementGroupConnectionSettings(serverName);
				mgSettings.UserName = name;
				mgSettings.Domain = userDomain;
				mgSettings.Password = password;

				Console.WriteLine("Connecting to the SDK Service as user: " + name);

				ManagementGroup mg = ManagementGroup.Connect(mgSettings);
				if (mg.IsConnected)
					Console.WriteLine("Connection succeeded.");
				else
					throw new InvalidOperationException("Not connected to an SDK Service.");
		}
			catch (ServerDisconnectedException sde)
			{
				Console.WriteLine("\nConnection failed. " + sde.Message);
				if (sde.InnerException != null)
					Console.WriteLine(sde.InnerException.Message);
			
				// Call custom method to prompt the user for credentials if needed.
			
		}
			catch (ServiceNotRunningException snr)
			{
				Console.WriteLine(snr.Message);

				// Make one attempt to start the service.
				System.ServiceProcess.ServiceController sc = new 
					ServiceController("localhost", serverName);

				Console.WriteLine("Attempting to start the SDK Service on " + serverName + ".");
				sc.Start();

				// Wait 20 seconds for it to enter the Running state.
				sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 20));

				if (sc.Status == ServiceControllerStatus.Running)
				{
					ManagementGroupConnectionSettings mgSettings =
					new ManagementGroupConnectionSettings(serverName);
					mgSettings.UserName = name;
					mgSettings.Domain = userDomain;
					mgSettings.Password = password;

					ManagementGroup mg = ManagementGroup.Connect(mgSettings);
					if (mg.IsConnected)
						Console.WriteLine("Connection succeeded.");
					else
						throw new InvalidOperationException("Not connected to an SDK Service.");
			}
				else
				{
					throw new InvalidOperationException("Unable to restart and connect to the SDK Service.");
			}
		}
	}
}
}

The code example requires a reference to the system.serviceprocess.dll file. This assembly is included in the Microsoft .NET Framework.

Instead of explicitly calling the Connect method, you can instantiate a new ManagementGroup object by using the new keyword, as shown in the following example:

mg = new ManagementGroup(mgSettings);

is the same as:

mg = ManagementGroup.Connect(mgSettings);

See Also