12 thoughts on “{Dynamics CRM+ Authentication}–Authentication with Dynamics CRM online Web API without user login screen– Where headless authentication works and where not?

  1. Hi Debajit,
    Tried couple of ways to test the console app, but getting authentication issues.

    opt1 – authorityUri=https://login.microsoftonline.com/jksoft123.crm4.dynamics.com/oauth2/authorize

    UserCredential userCredential = new UserCredential(“”, “”);
    string authorityUri = “https://login.microsoftonline.com/jksoft123.crm4.dynamics.com/oauth2/authorize”;
    TokenCache tokenCache = new TokenCache();
    AuthenticationContext context = new AuthenticationContext(authorityUri);
    AuthenticationResult result = context.AcquireToken(“https://jksoft123.crm4.dynamics.com”, “f3aaf9d8-baca-4f0d-a88d-1fa6e1ce694f”, userCredential);
    LoadAccounts(result.AccessToken);

    Error

    ‘authority’ is not in the list of valid addresses. unknown_error (login.microsoftonline.com):
    Access Denied

    Access Denied (authentication_failed)

    Your credentials could not be authenticated: “Credentials are missing.”. You will not be permitted access until your credentials can be verified.

    This is typically caused by an incorrect username and/or password, but could also be caused by network problems.

    For assistance, contact your network support team.

    opt2 –
    authorityUri=”https://login.windows.net/jksoft123.onmicrosoft.com/oauth2/authorize”;

    UserCredential userCredential = new UserCredential(“”, “”);
    string authorityUri = “https://login.windows.net/jksoft123.onmicrosoft.com/oauth2/authorize”;
    TokenCache tokenCache = new TokenCache();
    AuthenticationContext context = new AuthenticationContext(authorityUri);
    AuthenticationResult result = context.AcquireToken(“https://jksoft123.crm4.dynamics.com”, “f3aaf9d8-baca-4f0d-a88d-1fa6e1ce694f”, userCredential);
    LoadAccounts(result.AccessToken);

    Error

    user_realm_discovery_failed: User realm discovery failed
    {“The remote server returned an error: (407) Proxy Authentication Required.”}

    1. Hi,

      Just confirming, prior to running this code, did you do the following steps
      1. add your Dynamics Online directory to Azure
      2. When you registered your application with Dynamics Online AD, did you allow implicit authorization. Also I assume you tried this from a native client like Console or WPF
      3. Did you provide access to the Dynamics CRM online instance for your app?

      In case you have missed any of the above, complete that and try with option2 again. It should work. If not, let me know.

      Regards
      Debajit

  2. Hi Debajit, Thank you for your efforts in maintaining this tutorial. I am learning Dynamic CRM and trying to get the access token of the current session when user actively using online CRM instance. Every api talks only about taking token using url and callback. Can you guide me in right direction ?

    1. Hi Ramnivas,
      To get the token we have to use OAuth. Now OAuth itself is a URL based protocol based on client_id and client_secret. Now the question is when you are trying to access the token, it is from which context. Are you trying to access the token from within dynamics only or from an external application that connects with Dynamics?

      Cheers
      Debajit

  3. Hi Debajit
    Please help me out !!!
    1. I have Rest Api and its hosted on our Server
    2. I want to Integrate my Rest Api with Dynamic CRM 365.

    Scenario:-
    Suppose when user create any Accounts/Contacts in Dynamic CRM 365 and fill some value in Postal-Code/Zip-Code and click on Save Button.
    On Click of save button , validate this Postal-code through Rest Api which is hosted on other server and API send respose to the Dynamic CRM 365 which is valid Postal code or not

  4. Hi Debajit..Thanks for this tutorial which saved me a lot of time. I too was trying access CRM web APIs in a web application and had no luck to find an overload method which uses both client id and client secret (which is really needed for a web application) What I finally did was to self host WCF services in a console application and register it as a native app in AAD. It worked great!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s