Choosing CRM Connection Option

Choosing CRM Connection Option

Many CRM application that are written for Microsoft Dynamics CRM require to authenticate a user with any Microsoft Dynamics CRM deployment. This sample in this article demonstrates how to connect and authenticate a user with Microsoft Dynamics CRM On-Premise or Microsoft Dynamics CRM Online (using both Windows Live Id and Office 365 credentials).

The code example below contains three methods.
A public class method that establishes the connection, and two private methods to retrieve the Authentication Credentials and retrieve the Organization Service Proxy.

Many CRM application that are written for Microsoft Dynamics CRM require to atuneticate a user with any Microsoft Dynamics CRM deployment. This sample in this article demonstrates how to connect and authenticate a user with Microsoft Dynamics CRM On-Premise or Microsoft Dynamics CRM Online (using both Windows Live Id and Office 365 credentials).

The code example below contains three methods.
A public class method that establishes the connection, and two private methods to retrieve the Authentication Credentials and retrieve the Organization Service Proxy.

The following is the list of references, variables and properties that are required for this implementation to work.

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Client.Services;

public static OrganizationServiceProxy service;
private static string UserName { get; set; }

private static string Password { get; set; }

The ConnectToCRM creates a new OrganizationServiceProxy class that allows performing operations against all of the Microsoft Dynamics CRM environments.
The method retrieves the specified credentials and established the OrganizationServiceProxy class connection by calling the GetProxy method.

The serviceUrl contains the Uri of the CRM Services and the provider contains the type of provider that is going to be used to connect to CRM.
There are 5 options for the AuthenticationProviderType: Active Directory (1), Federation (2), Live Id (3), Online Federation (4) and None (0).

public static void ConnectToCRM(Uri serviceUrl, AuthenticationProviderType provider)
{
IServiceManagement<IOrganizationService> orgServiceManagement =
ServiceConfigurationFactory.CreateManagement<IOrganizationService>(serviceUrl);

AuthenticationCredentials credentials = GetCredentials(provider);
service = GetProxy<IOrganizationService, OrganizationServiceProxy>
(orgServiceManagement, credentials);
service.EnableProxyTypes();
}

The GetCredentials method returns an AuthenticationCredentials class contains the credentials of the CRM user based on the environment that they are connecting to. This method shows the implementation for Active Directory, CRM Online using Windows Live Id and CRM Online using Office 365.   

private static AuthenticationCredentials GetCredentials(AuthenticationProviderType provider)
{
AuthenticationCredentials credentials = new AuthenticationCredentials();
switch (provider)
{
case AuthenticationProviderType.ActiveDirectory: // Active Directory
credentials.ClientCredentials.Windows.ClientCredential =
new NetworkCredential(UserName, Password);
break;
case AuthenticationProviderType.LiveId: // CRM Online using Live Id             
credentials.ClientCredentials.UserName.UserName = UserName;

credentials.ClientCredentials.UserName.Password = Password;
credentials.SupportingCredentials = new AuthenticationCredentials();
credentials.SupportingCredentials.ClientCredentials =

Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();

         break;
case AuthenticationProviderType.OnlineFederation: // CRM Online using Office 365
credentials.ClientCredentials.UserName.UserName = UserName;
credentials.ClientCredentials.UserName.Password = Password;
break;
default:
credentials.ClientCredentials.UserName.UserName = UserName;
credentials.ClientCredentials.UserName.Password = Password;
break;
   }
   return credentials;
}

The following is a generic method that is used to obtain the discovery and organization service proxy.
The TService parameter is used to set the IDiscoveryService or IOrganizationService type to request the resepective service proxy instance.
The method will finally return either the DiscoveryServiceProxy or the OrganizationServiceProxy based on the TService type.

private static TProxy GetProxy<TService, TProxy>
(IServiceManagement<TService> serviceManagement,
AuthenticationCredentials authCredentials)
where TService : class           
where TProxy : ServiceProxy<TService>
{
Type classType = typeof(TProxy);
// Obtain service proxy for Windows LiveId and Office 365 environments.
if (serviceManagement.AuthenticationType != AuthenticationProviderType.ActiveDirectory)
{
AuthenticationCredentials tokenCredentials = serviceManagement.Authenticate(authCredentials);
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
.Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
   }

// Obtain service proxy for ActiveDirectory environment.
return (TProxy)classType
.GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
.Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
}

Once the connection to Microsoft Dynamics CRM is established, you can call any of the CRM WCF Service methods calls.
This example was created using the CRM 2011 SDK version 5.0.13 (published 1/7/2013).