web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

login web api or any solution to remote login to dynamic 365

(4) ShareShare
ReportReport
Posted on by 20

 

 

I'm looking for a solution—via web API or any other viable method—to authenticate users by sending credentials and retrieving the required authentication cookies for access to Dynamics 365. Although LDAP authentication works and I can log in to Windows, Dynamics 365 (On-Premise v9.1.2, Windows Authentication enabled) still returns 'unauthorized

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at
    Moved the question from the Dynamics 365 General to the CRM forum.
  • Daivat Vartak (v-9davar) Profile Picture
    7,835 Super User 2025 Season 2 on at
    Hello CU01041745-0,
     

    You're right to explore the Web API for programmatic authentication. However, with Windows Authentication enabled for your Dynamics 365 On-Premise v9.1.2, the standard Web API credential-based authentication flow (like OAuth 2.0) is not directly applicable for initial login and cookie retrieval in the way you might be thinking.

    Here's why and the viable methods you can use:

    Why Standard Web API Authentication Won't Work Directly with Windows Authentication:

    • Windows Authentication Mechanism: Windows Authentication relies on the user's Windows credentials (Kerberos or NTLM) being automatically passed to the web server by the browser. The server then authenticates the user against the domain controller. This process doesn't involve a standard username/password exchange via a web API call that returns authentication cookies in the traditional sense.
    • No Direct Credential Endpoint: The Dynamics 365 Web API doesn't expose a specific endpoint to send raw LDAP credentials (even though LDAP is involved in Windows Authentication on the backend) and receive authentication cookies. The authentication is handled at the IIS level before the Web API request even reaches the Dynamics 365 application logic.

    •  

    Viable Methods for Programmatic Authentication and Cookie Retrieval with Windows Authentication:

    The key is to leverage the existing Windows Authentication mechanism programmatically. Here are the common approaches:

    1. Using HttpClient with UseDefaultCredentials (.NET):

    If your application making the API calls is running within the same Windows domain as your users and the Dynamics 365 server, you can instruct the HttpClient to use the currently logged-in Windows user's credentials. This will automatically handle the Kerberos/NTLM negotiation.

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    public class DynamicsApiHelper
    {
        private readonly HttpClient _httpClient;
        private readonly Uri _dynamicsBaseUri;
        public DynamicsApiHelper(string dynamicsBaseUrl)
        {
            _dynamicsBaseUri = new Uri(dynamicsBaseUrl);
            _httpClient = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true });
        }
        public async Task<string> Get(string endpoint)
        {
            var response = await _httpClient.GetAsync(new Uri(_dynamicsBaseUri, endpoint));
            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine($"Error: {response.StatusCode} - {await response.Content.ReadAsStringAsync()}");
                return null;
            }
            return await response.Content.ReadAsStringAsync();
        }
        // ... other API methods (Post, Patch, etc.)
    }
    public class Example
    {
        public static async Task Main(string[] args)
        {
            string dynamicsUrl = "https://your-dynamics-server/your-org/"; // Replace with your Dynamics 365 URL
            var apiHelper = new DynamicsApiHelper(dynamicsUrl);
            string accounts = await apiHelper.Get("api/data/v9.1/accounts");
            if (accounts != null)
            {
                Console.WriteLine(accounts);
            }
        }
    }

    • How it works: When UseDefaultCredentials = true is set, HttpClient uses the Windows credentials of the process running the code to authenticate with the web server. IIS will recognize these credentials and, if authorized, allow access.
    • Cookie Handling: The HttpClient will automatically manage the necessary authentication cookies (if any are issued by IIS in this scenario) for subsequent requests within the same HttpClient instance. You don't need to explicitly retrieve and send them.
    • Limitations: This method requires the application to be running within the domain and under a Windows user context that has access to Dynamics 365.

    2. Programmatic Login via Browser Automation (Less Ideal for APIs):

    You could potentially automate a browser (e.g., using Selenium or Puppeteer) to navigate to the Dynamics 365 login page, have it handle the Windows Authentication challenge automatically, and then extract the authentication cookies from the browser's session.

    • How it works: The browser would initiate the connection, IIS would perform Windows Authentication, and the browser would store the resulting cookies. Your automation script would then retrieve these cookies.
    • Why it's Less Ideal for APIs:

      • Overhead: Browser automation is resource-intensive and slower than direct HTTP requests.
      • Complexity: It adds significant complexity to your application.
      • Maintenance: UI changes in Dynamics 365 could break your automation scripts.
      • Not a True API Approach: It's more of a UI scraping technique.  

    3. Integrated Windows Authentication with Web Requests (Non-.NET):

    If you're using other programming languages or tools, they likely have libraries that support Integrated Windows Authentication (IWA). You would configure your HTTP client to use the default system credentials.

    • Example (Python with requests-negotiate):
    import requests
    from requests_negotiate import HttpNegotiateAuth
    dynamics_url = "https://your-dynamics-server/your-org/"
    api_endpoint = "api/data/v9.1/accounts"
    try:
        response = requests.get(dynamics_url + api_endpoint, auth=HttpNegotiateAuth())
        response.raise_for_status()
        print(response.json())
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")

    • How it works: The requests-negotiate library handles the NTLM/Kerberos negotiation with the server using the system's default credentials.
    • Cookie Handling: Similar to HttpClient, the requests library will typically manage cookies automatically for subsequent requests.

    •  

    Why Your Existing Attempts Might Be Failing:

    • Direct Credential Submission: If you were trying to send username/password directly in a Web API call, IIS with Windows Authentication enabled wouldn't process it in the way a standard OAuth endpoint would. It expects the authentication to happen via the HTTP headers during the initial request negotiation.
    • Missing UseDefaultCredentials Equivalent: If you weren't using the equivalent of UseDefaultCredentials in your HTTP client, it wouldn't be attempting to leverage the Windows user's context for authentication.

    •  

    Key Takeaways and Recommendations:

    1. For applications running within the same Windows domain and under a user context with Dynamics 365 access, using HttpClient with UseDefaultCredentials (in .NET) or similar IWA libraries in other languages is the most straightforward and recommended approach. You don't need to explicitly retrieve and manage cookies.
    2. Avoid browser automation for API-level integration due to its overhead and complexity.
    3. Ensure the user account running your application (or the logged-in Windows user) has the necessary permissions within Dynamics 365.
    4. Your Dynamics 365 IIS configuration for Windows Authentication needs to be correct. This is usually handled during the initial setup of Dynamics 365.

    5.  

    Focus on configuring your HTTP client to utilize Integrated Windows Authentication, and you should be able to access the Dynamics 365 Web API without manually handling cookie retrieval after a separate login process. The authentication will happen transparently with each API request based on the Windows user context.

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans