Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Calling WebAPI results in 401 Unauthorized Error [C#]

Posted on by Microsoft Employee

Hi there

I'm using this sample:

https://msdn.microsoft.com/en-us/library/mt779074.aspx

I put in my credentials and try to connect to a CRM 2016 Organization but always get 401 Unauthorized.

IIS Authentication; Enabled: Anonymous, ASP.NET, Basic, Windows; Disabled: Digest, Forms.

Fiddler Screenshot:

fiddlo.png

Any help is much appreciated, really struggling with this one.

*This post is locked for comments

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Calling WebAPI results in 401 Unauthorized Error [C#]

    Hi Kim,

    Thank you for your feedback. I am glad that i could assist you.

    This should be possible with ADFS 2016 if i'm not mistaken.

    Please check a similar thread here: community.dynamics.com/.../268326 - maybe the code there can give you a starting point.

    Hope this helps

    Radu

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calling WebAPI results in 401 Unauthorized Error [C#]

    I am actually using IFD, mistake on my part. After setting it up correctly it is now working fine. You sir are a gentleman and a scholar, thank you very much.

    Only thing left now is to somehow pass the credentials while calling the API so I can execute a Console Application without having to fill up the username/passwort prompt.

  • Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Calling WebAPI results in 401 Unauthorized Error [C#]

    If you are not using IFD, are the SPNs set up properly?

  • Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Calling WebAPI results in 401 Unauthorized Error [C#]

    Hi,

    Are you using IFD? If so, I believe you need to register the app.

    Regards,

    Radu

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calling WebAPI results in 401 Unauthorized Error [C#]

    No, is this really needed for on-prem connections?

    I was following this guide:

    https://msdn.microsoft.com/en-us/library/mt770369.aspx#bkmk_prerequisites

    "Access to Dynamics Dynamics 365 Online or on-premises (or later). For all Dynamics 365 installation types, a user account with privileges to perform CRUD operations is required.

    In order to run samples against Dynamics 365 (online), you must register your application with Azure Active Directory to obtain a client ID and redirect URL. For more information, see Walkthrough: Register a Dynamics 365 app with Azure Active Directory."

    No mentioning of registering through Azure AD when using on-premises.

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Calling WebAPI results in 401 Unauthorized Error [C#]

    Hello,

    Did you also register the app in AD as per: msdn.microsoft.com/.../dn531010.aspx

    Besides the credentials you also need to specify the client id

    Hope this helps

    Radu

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans