We get ‘the request failed with HTTP status 401: Unauthorized’ error when connect to Dynamics CRM 4.0 On-premise server by using CRM 4.0 SDK. The server uses NTLM authentication with SSL support. The following is our code:
string server = "https://xxx.xxx.com";
string userid = "xxx";
string password = "xxx";
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.UseDefaultCredentials = false;
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(server), "NTLM", new NetworkCredential(userid, password, domain));
discoveryService.Credentials = cache;
discoveryService.Url = server + "/mscrmservices/2007/ad/crmdiscoveryservice.asmx";
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);
foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
{
Console.WriteLine(orgDetail.OrganizationName.ToString());
}