Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Create CrmServiceClient with AuthType=OAuth

(0) ShareShare
ReportReport
Posted on by 22

Hello everybody

I am trying to create an API that gets information from our D365 CRM environment in the cloud. I followed the instructions on https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/xrm-tooling/use-connection-strings-xrm-tooling-connect to create a connection string with AuthType=OAuth (because Office365 is deprecated) but I can't seem te create a new CrmServiceClient.

My code...

ConnectionString:

<add name="CdsConnection" connectionString="AuthType=OAuth;Username=myusername;Password=mypassword;Url=">kdg.crm4.dynamics.com;AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;RedirectUri=app:/.../58145B91-0C36-4500-8554-080854F2AC97;LoginPrompt=Auto" />

Creating of the CrmServiceClient:

crmSvc = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CdsConnection"].ConnectionString);

Result: crmSvc gets created, with a lot of errors in it.

  • CurrentAccessToken = 'crmSvc.CurrentAccessToken' threw an exception of type 'System.NullReferenceException'
  • EnvironmentId = 'crmSvc.EnvironmentId' threw an exception of type 'System.MissingMethodException'
  • LastCrmError = "Unable to connect to CRM: Methode niet gevonden: System.String Microsoft.Xrm.Sdk.Organization.OrganizationDetail.get_EnvironmentId().
    Methode niet gevonden: System.String Microsoft.Xrm.Sdk.Organization.OrganizationDetail.get_EnvironmentId().Unable to Login to Dynamics CRM
    Unable to Login to Dynamics CRMOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is null************ NullReferenceException - WhoAmI : Execute (WhoAmI) request to CRM from IOrganizationService |=> De objectverwijzing is niet op een exemplaar van een object ingesteld.
    De objectverwijzing is niet op een exemplaar van een object ingesteld.[TerminalFailure Failed to Execute Command - WhoAmI : RequestID=8f8bbada-fcd6-473d-a56d-a3ee180bab15 : Execute (WhoAmI) request to CRM from IOrganizationService duration=00:00:00.0036163 ExceptionMessage = De objectverwijzing is niet op een exemplaar van een object ingesteld.
    De objectverwijzing is niet op een exemplaar van een object ingesteld.Failed to Executed Command - WhoAmIRequest : RequestId=8f8bbada-fcd6-473d-a56d-a3ee180bab15 : total duration: 00:00:00.0066912
    Failed to Executed Command - WhoAmIRequest : RequestId=8f8bbada-fcd6-473d-a56d-a3ee180bab15 : total duration: 00:00:00.0066912************ Exception - Failed to lookup current user
    De objectverwijzing is niet op een exemplaar van een object ingesteld.OrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
    OrganizationWebProxyClient is null'
  • LastCrmException = {"OrganizationWebProxyClient is null"}
  • TenantId = 'crmSvc.TenantId' threw an exception of type 'System.MissingMethodException'

I've tried many changes in the meantime, but can't seem to figure out what is going on. Has anyone out there have got an idea?

Thanks in advance!

Kind regards
Thomas Van Dyck

  • Suggested answer
    Pradeep Rai Profile Picture
    5,487 Super User 2025 Season 1 on at
    RE: Create CrmServiceClient with AuthType=OAuth

    Hi Peter Forsbom,

    As you know we can setup the redirect URI while registering App on Azure and same URI we can use in console.

    But for development purpose Microsoft give predefine URI as shown below:

    pastedimage1632215937151v1.png

    So, you can use the same.

    For complete guide refer below link,

    https://docs.microsoft.com/en-us/powerapps/developer/data-platform/xrm-tooling/use-connection-strings-xrm-tooling-connect

    Thanks,

    Pradeep.

  • PeterForsbom Profile Picture
    9 on at
    RE: Create CrmServiceClient with AuthType=OAuth

    Hi

    May I ask just how you configure the connections string. I know where to get the AppId but how do I create the RedirectUri with the format "app://{where is this GUID coming from?}"

  • Community Member Profile Picture
    on at
    RE: Create CrmServiceClient with AuthType=OAuth

    Hello All,

    As per latest Microsoft recommendation we are not supposed to use “AuthType=Office365”.

    docs.microsoft.com/.../authenticate-office365-deprecation

    We can use Application Account (Client ID & Secret Key) to generate Token & access Dynamics CRM Organization Service. But if you want to use User ID & PWD then Use (AuthType = OAuth)

    Sample Code :

    string connectionString = "AuthType = OAuth; Url = 'https://*****.crm.dynamics.com'; Username = '*******'; Password = '*******'; AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d; RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;LoginPrompt=Never";

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    CrmServiceClient crmServiceClient = new CrmServiceClient(connectionString);

    WhoAmIResponse whoAmIResponse = crmServiceClient.Execute(new WhoAmIRequest()) as WhoAmIResponse;

    Note : While trying to use this from Azure Function I got below errors :

    1) ERROR REQUESTING Token FROM THE Authentication contextNeed a non-empty authority

    2) One or more errors occurred. => An error occurred while sending the request. => The underlying connection was closed: An unexpected error occurred on a send. => Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. => An existing connection was forcibly closed by the remote hostERROR REQUESTING Token FROM THE Authentication context

    3) CurrentAccessToken = 'crmServiceClient.CurrentAccessToken' threw an exception of type 'System.NullReferenceException'

    Easily you can resolve these using just one additional line  :    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    Ref URL : support.microsoft.com/.../4051700  

    Microsoft Dynamics 365 Customer Engagement (online) to require TLS 1.2 for connectivity

    Pls let me know if you are facing any other issues.

    Thanks,

    Sumit

  • Thomas Van Dyck Profile Picture
    22 on at
    RE: Create CrmServiceClient with AuthType=OAuth

    Thanks for your reply Bipin Kumar. Your code was really helpfull for me to understand what was going on exactly!

  • Verified answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Create CrmServiceClient with AuthType=OAuth

    Hi,

    I have just tried from my console application with same configuration as yours and for me it is working.

    Check xrm.tooling dll version - 9.0.2.23

    check .net framework

    I have placed my console working code at below github url. you can replace your credential and try that.

    github.com/.../WorkWithCsharp

    If found helpful, Please mark my answer verified.

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

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 82 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans