Hello Folks, Finally I was able to spend some time out to work on shorter code for CRM connection using late bound approach. More than the helper concept the idea is about a universal application which can help you troubleshoot/isolate the issues with CRM authentication.

The blog as an application attached where you will also see the activity log for the events that take place. Here's the screenshot

Happy Learning.

//.NET DLLS

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel.Description;

 

//CRM SDK DLLS

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Xrm.Sdk.Discovery;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk.Query;

 

 

 

namespace CRM_Auth_Troubleshooter

{

    publicclassCrmConnector

    {

        //Using Global Variables : Credentials

 

        publicString GlobalUser = string.Empty;

        publicString GlobalPass = string.Empty;

        privateString GlobalDomain = string.Empty;

 

        ///<summary>

        /// Using the service to discover the organizations

        ///</summary>

        ///<param name="discoveryUrl"></param>

        ///<returns></returns>

        publicOrganizationDetailCollection DiscoverOrganizations(string discoveryUrl)

        {

            IDiscoveryService service = GetDiscoveryProxy(discoveryUrl);

 

            RetrieveOrganizationsRequest orgsRequest = newRetrieveOrganizationsRequest()

                        {

                            AccessType = EndpointAccessType.Default,

                            Release = OrganizationRelease.Current

                        };

            RetrieveOrganizationsResponse organizations = (RetrieveOrganizationsResponse)service.Execute(orgsRequest);

            return organizations.Details;

        }

 

        // <summary>

        /// This function will return the Type of proxy Specified

        ///</summary>

        ///<typeparam name="TService"></typeparam>

        ///<typeparam name="TProxy"></typeparam>

        ///<param name="serviceManagement"></param>

        ///<param name="authCredentials"></param>

        ///<param name="Url"></param>

        ///<param name="IsDiscovery"></param>

        ///<returns></returns>

        private TProxy GetProxy<TService, TProxy>(IServiceManagement<TService> serviceManagement, AuthenticationCredentials authCredentials,

            String Url, Boolean IsDiscovery)

            where TService : class

            where TProxy : ServiceProxy<TService>

        {

            Type classType = typeof(TProxy);

            serviceManagement = ServiceConfigurationFactory.CreateManagement<TService>(newUri(Url));

            AuthenticationProviderType OrgAuthType = serviceManagement.AuthenticationType;

            authCredentials = GetCredentials(OrgAuthType);

            AuthenticationCredentials tokenCredentials = serviceManagement.Authenticate(authCredentials);

            SecurityTokenResponse responseToken = tokenCredentials.SecurityTokenResponse;

            if (IsDiscovery)

            {

                return (TProxy)classType

                    .GetConstructor(newType[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })

                    .Invoke(newobject[] { serviceManagement, responseToken });

            }

            else

                return (TProxy)classType

                    .GetConstructor(newType[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })

                    .Invoke(newobject[] { serviceManagement, authCredentials.ClientCredentials });

        }

 

        ///<summary>

        /// Returns the discovert service proxy

        ///</summary>

        ///<param name="discoverUrl"></param>

        ///<returns></returns>

        privateIDiscoveryService GetDiscoveryProxy(string discoverUrl)

        {

            //DiscoveryServiceProxy discoverProxy = null;

            IServiceManagement<IDiscoveryService> discoverManagement = null;

            AuthenticationCredentials authCredentials = null;

            return GetProxy<IDiscoveryService, DiscoveryServiceProxy>(discoverManagement, authCredentials, discoverUrl, true);

        }

        ///<summary>

        /// Returns the Organization Service Proxy

        ///</summary>

        ///<param name="organizationUrl"></param>

        ///<returns></returns>

        privateIOrganizationService GetOrganizationProxy(string organizationUrl)

        {

            //DiscoveryServiceProxy discoverProxy = null;

            IServiceManagement<IOrganizationService> orgManagement = null;

            AuthenticationCredentials authCredentials = null;

            return GetProxy<IOrganizationService, OrganizationServiceProxy>(orgManagement, authCredentials, organizationUrl, true);

        }

 

        //Note: Please use your own authentication mechanism. Refer to this link for more types: http://msdn.microsoft.com/en-us/library/gg309393.aspx

        ///<summary>

        /// This function retuns the authentication instance for different type of modes.

        ///</summary>

        ///<param name="endpointType"></param>

        ///<returns></returns>

        privateAuthenticationCredentials GetCredentials(AuthenticationProviderType endpointType)

        {

            AuthenticationCredentials authCredentials = newAuthenticationCredentials();

            switch (endpointType)

            {

 

                caseAuthenticationProviderType.ActiveDirectory:

                    string userDomain = GlobalUser.Split('\\').GetValue(0).ToString();

                    string userNameSplit = GlobalUser.Split('\\').GetValue(1).ToString();

 

                    authCredentials.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(userNameSplit, GlobalPass, userDomain);

                    break;

 

                default: // For Federated and OnlineFederated environments.                   

                    authCredentials.ClientCredentials.UserName.UserName = GlobalUser;

                    authCredentials.ClientCredentials.UserName.Password = GlobalPass;

                    // For OnlineFederated single-sign on, you could just use current UserPrincipalName instead of passing user name and password.

                    // authCredentials.UserPrincipalName = UserPrincipal.Current.UserPrincipalName;  //Windows/Kerberos

                    break;

 

            }

            return authCredentials;

        }

          ///<summary>

        /// Based on the Organization URL specified. The string will return the logged user credentials

        ///</summary>

        ///<param name="organizationUrl"></param>

        ///<returns></returns>

        publicstring GetLoggedUser(string organizationUrl)

        {

            IOrganizationService serviceProxy = this.GetOrganizationProxy(organizationUrl);

            Guid userid = ((WhoAmIResponse)serviceProxy.Execute(new WhoAmIRequest())).UserId;

            Entity systemUserEntity = serviceProxy.Retrieve("systemuser", userid, new ColumnSet(true));

            return systemUserEntity.GetAttributeValue<String>("fullname").ToString();

        }

    }

}