Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

CrmServiceClient not working for OAuth type

Posted on by 310

Hi Team,

Please find find below is the connection string format where OAuth not working. 

public static CrmServiceClient Connection()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

string authType = "OAuth";
string userName = "abcd@d365.onmicrosoft.com";
string password = "12345";

string url = "">abcd.crm8.dynamics.com";

string appId = "51f81489-12ee-4a9e-aaae-a2591f45987d";
string reDirectURI = "app://58145B91-0C36-4500-8554-080854F2AC97";
string loginPrompt = "Never";

string ConnectionString = string.Format("AuthType ={0};Username ={1};Password ={2};Url ={3};AppId={4};RedirectUri={5};LoginPrompt={6}",
authType, userName, password, url, appId, reDirectURI, loginPrompt);

CrmServiceClient svc = new CrmServiceClient(ConnectionString);
return svc;
}

I have tried below web.config option also :

<appSettings>

<add key="CRMConnectionString" value="AuthType=OAuth; Username=abcd@d365.onmicrosoft.com; Password=12345; Url=https://abcd.crm8.dynamics.com; AppId=51f81489-12ee-4a9e-aaae-a2591f45987d; RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97; LoginPrompt=Never"/>

</appSettings>

Problem :- When I trying to call above function is will give below error, I have tried multiple blogs but that didn't work in my case.

Unable to Login to Dynamics CRMOrganizationWebProxyClient is null
OrganizationWebProxyClient is nullOrganizationWebProxyClient is null
OrganizationWebProxyClient is nullOrganizationWebProxyClient is null

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CrmServiceClient not working for OAuth type

    Hello, you trying to connect use crm account. 

    and i hope you share error message. 

    anyway

    if you connect using crm account, check this set true.  ( must set allowpublicclient true)

    portal azure -> AAD -> App Regstration -> manifest -> allowpublicclient

    pastedimage1649728434862v1.png

    if you can't keep connect  set this variable true (it use openidconnect in my case) just in case.... idk 

    pastedimage1649728599235v2.png

        post that way is use application user. its different.

    and check you api permission too

    pastedimage1649730133093v1.png

    i share my code i hope it help to you


    /// 
    /// Connect with specific crm user.
    /// You must set AllowPublicClient in AAD, AppRegistration, Manifest.
    /// 
    /// https://yourorg.crm.dynamics.com
    /// from aad, app registration
    /// crm id
    /// crm pw
    /// from aad, app registration 
    /// 
    public (CrmServiceClient, Guid) ConnectServiceOAuth(string environmentUri, string clientId, string id, string pw, string tenantId)
    {
    string conn = $@"
    Url = {environmentUri};
    AuthType = {Microsoft.Xrm.Tooling.Connector.AuthenticationType.OAuth:G};
    Username = {id};
    Password = {pw};
    AppId = {clientId};
    RedirectUri = app://{tenantId};
    RequireNewInstance = True;
    LoginPrompt=Auto;"; //GenerateConString();
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    
    //실행 대기시간 default 2분 -> 5분
    CrmServiceClient.MaxConnectionTimeout = new TimeSpan(0, 5, 0);
    
    var svc = new CrmServiceClient(conn);
    if (svc.IsReady) //Connection is successful
    {
    
    }
    else
    {
    throw svc.LastCrmException;
    }
    
    return (svc, svc.GetMyCrmUserId());
    }
    
    /// 
    /// If you are connecting using an secret configured for the application, you will use the "ClientCredential" class passing in the 'clientId' and 'clientSecret' rather than a "UserCredential" with 'userName' and 'password' parameters.
    /// it need application user on CRM(have to create)
    /// 
    /// 
    /// 
    /// https://yourorg.crm.dynamics.com
    /// from aad, app registration
    /// from aad, app registration
    /// 
    public (CrmServiceClient, Guid) ConnectServiceApplicationUser(string environmentUri, string clientId, string clientSecret)
    {
    string conn = $@"
    Url = {environmentUri};
    AuthType = {Microsoft.Xrm.Tooling.Connector.AuthenticationType.ClientSecret:G};
    ClientId = {clientId};
    ClientSecret = {clientSecret};
    RequireNewInstance = True; LoginPrompt=Never;"; //GenerateConString();
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    
    //실행 대기시간 default 2분 -> 5분
    CrmServiceClient.MaxConnectionTimeout = new TimeSpan(0, 5, 0);
    
    var svc = new CrmServiceClient(conn);
    if (svc.IsReady) //Connection is successful
    {
    
    }
    else
    {
    throw svc.LastCrmException;
    }
    
    return (svc, svc.GetMyCrmUserId());
    }
    
    /// 
    /// Connect using a certificate thumbprint
    /// If you are connecting using a certificate and using the Microsoft.Xrm.Tooling.Connector.CrmServiceClient you can use this
    /// 인증서의 지문을 이용하여 사용한다,
    /// win   r -> certmgr.msc -> 개인용 -> 인증서 선택 -> 제일 밑의 지문(thumbprint)
    /// 
    /// 
    /// 
    /// e.g.) DC6C689022C905EA5F812B51F1574ED10F256FF6
    /// e.g.) https://yourorg.crm.dynamics.com
    ///  e.g.) 545ce4df-95a6-4115-ac2f-e8e5546e79af
    public static Guid ConnectServiceThumbprint(string certThumbPrintId, string environmentUri, string clientId)
    {
    string ConnectionStr = $@"AuthType=Certificate;
    SkipDiscovery=true;url={environmentUri};
    thumbprint={certThumbPrintId};
    ClientId={clientId};
    RequireNewInstance=true";
    
    ////실행 대기시간 default 2분 -> 5분
    //CrmServiceClient.MaxConnectionTimeout = new TimeSpan(0, 5, 0);
    CrmServiceClient svc = new CrmServiceClient(ConnectionStr);
    
    if (svc.IsReady)
    {
    //실행 대기시간 default 2분 -> 5분
    //if (svc.OrganizationWebProxyClient != null)
    //{
    // svc.OrganizationWebProxyClient.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
    //}
    //else if(svc.OrganizationServiceProxy != null)
    //{
    // svc.OrganizationServiceProxy.Timeout = new TimeSpan(0, 5, 0);
    //}
    }
    return ((WhoAmIResponse)svc.OrganizationServiceProxy.Execute(new WhoAmIRequest())).UserId;
    }

  • Suggested answer
    furkank Profile Picture
    furkank 535 on at
    RE: CrmServiceClient not working for OAuth type

    Hi Manoj, Are you sure your AppId is correct? Where do you get your AppId?

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,962 Moderator on at
    RE: CrmServiceClient not working for OAuth type

    Hi,

    Have you tried updating the nuget package to latest version?

    docs.microsoft.com/.../microsoft.xrm.tooling.connector

    Also, try to remove below property from your connection string.

    LoginPrompt=Never

    I tried your code in my console application and it works. Please download my console app from my guthub page and replace Url, Username and Password to check if it works for you.

    github.com/.../WorkWithCsharp

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Suggested answer
    RE: CrmServiceClient not working for OAuth type

    Check the following for the correction method.

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

    It may also be related to the version of CrmServiceClient.

    www.nuget.org/.../Microsoft.CrmSdk.CoreTools

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CrmServiceClient not working for OAuth type

    Hi,

    Please follow the below blog and YouTube channel you will get your answer yourself.

    https://juniorazurecrm.blogspot.com/

    https://www.youtube.com/watch?v=X7H25FZHoF0&t=3s

    Once you get your answer please like and subscribe my channel and follow the blog.

    Thanks,

    Arshad

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans