Partilhar via


Authenticate Office 365 Users with Microsoft Dynamics CRM Online

Here’s what I have for you guys a simple technique to connect to your CRM office 365 based organizations. I have created a sample to add a new contact. This will help you use the Create function of CRM Service

  • Create an empty solution in VS 2010
  • Right click, to add new class library project
  • Choose the Target Framework as .NET 4.0
  • Name the project as “<<your preferred name>>”
  • Add the required references to your project
  • Add the following code:

Note: The example is based out from MSDN article. For complete details on how to create a helperclass, please refer to https://msdn.microsoft.com/en-us/library/gg309393.aspx.

Walkthrough

  • Create an empty solution in VS 2010
  • Right click, to add new console
  • Choose the Target Framework as .NET 4.0
  • Name the project as “CrmProxy”
  • Create a class called
  • Add the required references to your project
  • Add the following code:

public class Program

    {

        //Define global variables

        private String discoveryServiceAddress = "https://disco.crm5.dynamics.com/XRMServices/2011/Discovery.svc"; //Use this discovery URL

        private String organizationUniqueName = "UniqueName"; //Provide the unique name of the organization

        private String userName = "user@yourname.onmicrosoft.com"; //Use your office 365 user Id

        private String password = "*******"; //Provide your password here

        private String domain = "domainName";

        static void Main(string[] args)

        {

            Program programObject = new Program();

            programObject.ReturnCRMServiceInstance();

        }

        /// <summary>

        /// This function returns the instance for CRM Service using OrganizationServiceProxy

        /// </summary>

        /// <returns></returns>

        public OrganizationServiceProxy ReturnCRMServiceInstance()

        {

            String organizationUri = "https://<yourorgName>.api.crm5.dynamics.com/XRMServices/2011/Organization.svc";

            IServiceManagement<IOrganizationService> OrganizationServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(organizationUri));

            AuthenticationProviderType OrgAuthType = OrganizationServiceManagement.AuthenticationType;

            AuthenticationCredentials authCredentials = GetCredentials(OrgAuthType);

            AuthenticationCredentials tokenCredentials = OrganizationServiceManagement.Authenticate(authCredentials);

            OrganizationServiceProxy organizationProxy;

            SecurityTokenResponse responseToken = tokenCredentials.SecurityTokenResponse;

            using (organizationProxy = new OrganizationServiceProxy(OrganizationServiceManagement, responseToken))

            {

                organizationProxy.EnableProxyTypes();

            }

            return organizationProxy;

        }

        /// <summary>

        /// Use this function for creating

        /// </summary>

        /// <param name="lastName"></param>

        /// <returns></returns>

        public Guid createContact(string lastName)

        {

            OrganizationServiceProxy organizationProxy = this.ReturnCRMServiceInstance();

            Entity contactEntity = new Entity("contact");

            contactEntity.Attributes["lastname"] = lastName;

            return organizationProxy.Create(contactEntity);

        }

        //Note: Please use your own authentication mechanism. Refer to this link for more types: https://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>

        private AuthenticationCredentials GetCredentials(AuthenticationProviderType endpointType)

        {

            AuthenticationCredentials authCredentials = new AuthenticationCredentials();

            switch (endpointType)

            {

                case AuthenticationProviderType.ActiveDirectory:

                    authCredentials.ClientCredentials.Windows.ClientCredential =

                        new System.Net.NetworkCredential(userName, password, domain);

                    break;

                case AuthenticationProviderType.LiveId:

                    authCredentials.ClientCredentials.UserName.UserName = userName;

                    authCredentials.ClientCredentials.UserName.Password = password;

                    authCredentials.SupportingCredentials = new AuthenticationCredentials();

                    authCredentials.SupportingCredentials.ClientCredentials =

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

                    break;

                default: // For Federated and OnlineFederated environments.

                    authCredentials.ClientCredentials.UserName.UserName = userName;

                    authCredentials.ClientCredentials.UserName.Password = password;

                    // 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;

        }

    }

 

  • Now, we’re all set.
  • Build the complete solution and now you have your assembly ready.
  • You can use this in any of your projects.

Happy Integration with Office 365!

Cheers,
Apurv

Program.zip

Comments

  • Anonymous
    August 01, 2012
    Cheers and thanks :)

  • Anonymous
    August 15, 2012
    I tried with my org, the ServiceConfigurationFactory class continues to return the authentication type as liveid and fails in authentication.The account is created with office365

  • Anonymous
    August 21, 2012
    Hi Chimanrao,Can you try creating a trial organization and check this code? Also when did you create your organization? For more details on your organization, request you to report this to MS Support.

  • Anonymous
    April 09, 2013
    Thanks for sharing this blog. This article will be very useful for Microsoft Dynamics CRM online users.

  • Anonymous
    October 20, 2014
    Thank you. I spent a lot of time to understand how to connect to CRM 2013 Online before this article.

  • Anonymous
    January 21, 2015
    Thanks Apurva, for this blog, it is really good. Just have one small question, why we don't use device credential for office 365 version?

    • Anonymous
      October 23, 2016
      DeviceCredentials has been deprecated with Microsoft account, since office 365 handles authentication itself.
  • Anonymous
    August 26, 2016
    The comment has been removed

    • Anonymous
      October 23, 2016
      are you using CRM 2016? if so, this code may not apply. Thanks for the feedback. I will add the disclaimer.
      • Anonymous
        January 12, 2017
        Hi Apurv,Yes, I am also facing the same issue, and yes, I am having CRM 2016 online. Is there any alternate approach to get service proxy?
        • Anonymous
          January 14, 2017
          Can you try using this? There is a library available here and see if that works? What's the error you are seeing?