Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

CrmServiceClient connection stopped working today

(0) ShareShare
ReportReport
Posted on by 255

Hi.
I've been using the CrmServiceClient with Tls12 in C# for a while now but last night it stopped working.
The LastCRMError is "Unable to Login to Dynamics CRM"
I haven't made any changes to this and testing the connection with XrmToolBox succeeds.

Any ideas


ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

CrmServiceClient service = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: CrmServiceClient connection stopped working today

    Hi,

    Try below code and you should be able to connect to CRM from console/c#

    // e.g. yourorg.crm.dynamics.com

               string url = "";

               // e.g. you@yourorg.onmicrosoft.com

               string userName = "";

               // e.g. y0urp455w0rd

               string password = "";

               string conn = $@"Url = {url};AuthType = OAuth;UserName = {userName};Password = {password};

                                AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;

                                RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;

                                LoginPrompt=Never;RequireNewInstance = True";

               System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

               var svc = new CrmServiceClient(conn);

    AppId and RedirectUri is provided by microsoft and it will work for all crm online instance.

    Please mark my answer verified if i were helpful

  • TKoller Profile Picture
    TKoller 181 on at
    RE: CrmServiceClient connection stopped working today

    ok so lets take the bazooka :-)

    here is some code to connect with clientid/clientsecret

    We are using this for some month now without any problems.

    https://stackoverflow.com/questions/36160766/how-do-i-connect-a-server-service-to-dynamics-online

    this is the class


    public class DynamicsCeClientIdClient
        {
            public string TenantId = "";                 // from your app registration overview "Directory (tenant) ID"
            public string ClientId = "";                     // from your app registration overview "Application (client) ID"
            public string ClientSecret = "";             // secret generated in step 1
            public string LoginUrl = "https://login.microsoftonline.com";    // aad login url
            public string OrganizationName = "";     // check your dynamics login url, e.g. https://..dynamics.com
            public string OrganizationRegion = "crm4"; // might be crm for north america, check your dynamics login url    
    
            public DynamicsCeClientIdClient(string organizationName, string clientId, string clientSecret, string tenantId, string region = "crm4")
            {
                TenantId = tenantId;
                ClientId = clientId;
                ClientSecret = clientSecret;
                OrganizationName = organizationName;
                OrganizationRegion = region;
            }
    
            private string GetServiceUrl()
            {
                return $"{GetResourceUrl()}/XRMServices/2011/Organization.svc/web";
            }
    
            private string GetResourceUrl()
            {
                return $"https://{OrganizationName}.api.{OrganizationRegion}.dynamics.com";
            }
    
            private string GetAuthorityUrl()
            {
                return $"{LoginUrl}/{TenantId}";
            }
    
            public async Task CreateClient(string sdkVersion = "9.1")
            {
                var context = new AuthenticationContext(GetAuthorityUrl(), false);
                context.ExtendedLifeTimeEnabled = true;
                var token = await context.AcquireTokenAsync(GetResourceUrl(), new ClientCredential(ClientId, ClientSecret));
    
                return new OrganizationWebProxyClient(new Uri(GetServiceUrl()), true)
                {
                    HeaderToken = token.AccessToken,
                    SdkClientVersion = "9.1"
                };
            }
    
            public async Task CreateContext()
            {
                var client = await CreateClient();
                return new OrganizationServiceContext(client);
            }
    
            public async Task TestApiCall()
            {
                var context = await CreateContext();
    
                // send a test request to verify authentication is working
                var response = (WhoAmIResponse)context.Execute(new WhoAmIRequest());
            }
        }

    and this is how you create the client:

    var organization = "myorg";
    var clientId = "myclientid";
    var clientSecret = "myclientsecret";
    var tenantId = "mytenantid";
    var region = "crm4";
    
    var client = new DynamicsCeClientIdClient(organization, clientId, clientSecret, tenantId, region);
    
    var c = client.CreateClient("9.1").Result;

  • Stuie Profile Picture
    Stuie 255 on at
    RE: CrmServiceClient connection stopped working today

    Are you able to use my console code to connect to your instance(s) of dynamics?

  • Stuie Profile Picture
    Stuie 255 on at
    RE: CrmServiceClient connection stopped working today

    I dont have fiddler

  • Stuie Profile Picture
    Stuie 255 on at
    RE: CrmServiceClient connection stopped working today

    Hi. I've had a look at this but it seems a lot of work to resolve what is apparently an easy fix. At least until 2022

  • TKoller Profile Picture
    TKoller 181 on at
    RE: CrmServiceClient connection stopped working today

    and make sure fiddler is not running :-P this can also cause connection issues.

  • TKoller Profile Picture
    TKoller 181 on at
    RE: CrmServiceClient connection stopped working today

    I've send you an message.

  • necsa Profile Picture
    necsa 3,455 on at
    RE: CrmServiceClient connection stopped working today

    Hi Stuie,

    check the following link

    https://exptechsolutions.blogspot.com/2018/02/d365-crm-v9-troubleshooting-tls12-ssrs.html

    hope help you

  • Stuie Profile Picture
    Stuie 255 on at
    RE: CrmServiceClient connection stopped working today

    tried that too :(

  • TKoller Profile Picture
    TKoller 181 on at
    RE: CrmServiceClient connection stopped working today

    usually I only use this url:

    myorg.crm4.dynamics.com

    without api or anything else

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans