Condividi tramite


How to Connect to an SDK Service by Using Specified User Credentials

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

An SDK client can connect to the SDK Service by using a given set of user credentials.

Note

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 SDK Service.

Example

The following example demonstrates how to connect to the SDK 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.");
                }
            }
        }
    }
}
' Prompts for a username, domain, and password, and connects to the 
' SDK Service by using the supplied credentials.
Imports System
Imports System.Collections.Generic
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.VisualBasic
Imports System.Security
Imports System.ServiceProcess
Imports System.Text


Namespace SDKSamples
    Class Program

        Public Overloads Shared Function Main(ByVal args() As String) As Integer
            ' Define the server that you want to connect to.
            Dim serverName As String = "serverName"

            Console.WriteLine("Please specify the credentials to use when connecting to the SDK Service.")
            ' Get name.
            Console.Write("User name: ")
            Dim name As String = Console.ReadLine()

            ' Get domain.
            Console.Write("Domain: ")
            Dim userDomain As String = Console.ReadLine()

            ' Get password.
            Dim password As New SecureString()
            Console.Write("Password: ")
            While True
                ' Display asterisks for entered characters.
                Dim cki As ConsoleKeyInfo = Console.ReadKey(True)

                ' If password is complete, connect with supplied credentials.
                If cki.Key = ConsoleKey.Enter Then
                    Console.Write(Environment.NewLine)
                    Exit While
                Else
                    If cki.Key = ConsoleKey.Backspace Then
                        ' Remove the last asterisk from the console.
                        If password.Length > 0 Then
                            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
                            Console.Write(" ")
                            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
                            password.RemoveAt((password.Length - 1))
                        End If
                    Else
                        password.AppendChar(cki.KeyChar)
                        Console.Write("*")
                    End If
                End If
            End While

            Try
                Dim mgSettings As New ManagementGroupConnectionSettings(serverName)
                mgSettings.UserName = name
                mgSettings.Domain = userDomain
                mgSettings.Password = password

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

                Dim mg As ManagementGroup = ManagementGroup.Connect(mgSettings)
                If mg.IsConnected Then
                    Console.WriteLine("Connection succeeded.")
                Else
                    Throw New InvalidOperationException("Not connected to an SDK Service.")
                End If
            Catch sde As ServerDisconnectedException
                Console.WriteLine(ControlChars.Lf.ToString() & "Connection failed. " & sde.Message)
                If Not (sde.InnerException Is Nothing) Then
                    Console.WriteLine(sde.InnerException.Message)
                End If
                ' Call custom method to prompt the user for credentials if needed.
            Catch snr As ServiceNotRunningException
                Console.WriteLine(snr.Message)

                ' Make one attempt to start the service.
                Dim 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 Then
                    Dim mgSettings As New ManagementGroupConnectionSettings(serverName)
                    mgSettings.UserName = name
                    mgSettings.Domain = userDomain
                    mgSettings.Password = password

                    Dim mg As ManagementGroup = ManagementGroup.Connect(mgSettings)
                    If mg.IsConnected Then
                        Console.WriteLine("Connection succeeded.")
                    Else
                        Throw New InvalidOperationException("Not connected to an SDK Service.")
                    End If
                Else
                    Throw New InvalidOperationException("Unable to restart and connect to the SDK Service.")
                End If
            End Try
        End Function 'Main
    End Class 'Program
End Namespace 'SDKSamples

The code example requires a reference to the system.serviceprocess.dll file. This assembly is included in the .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

Concepts

Getting Started Developing an SDK Client Application

Other Resources

How to Connect an Operations Manager SDK Client to the SDK Service