Search code examples
c#web-servicessoap-clientnavision

C# Dynamics Nav WebService unable to get authorization


Im developing a project that calls multiple methods of web services from Dynamics Nav. It uses a SOAP request and sends an response xml to do the call.

I'm unable to authenticate to the web service as I get the following response:

  • response {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Thu, 31 Aug 2017 08:12:15 GMT Server: Microsoft-HTTPAPI/2.0 WWW-Authenticate: Negotiate Content-Length: 0 }} System.Net.Http.HttpResponseMessage

My sample of credentials code is:

var credentials = Encoding.ASCII.GetBytes(usuarioText + ":" + passText);
client.DefaultRequestHeaders.Authorization = new 
    AuthenticationHeaderValue("Windows", Convert.ToBase64String(credentials));

I need windows authentication for calling the nav service, I think I'm doing something wrong sending the credentials...


Solution

  • If the Service Tier is configured to use Windows Authentication, there are two ways to Authentication against Dynamics NAV Web Service.

    Using your current Credentials:

    // Creates instance of service and sets credentials.
    Customer_Service service = new Customer_Service();
    service.UseDefaultCredentials = true;
    

    Or setting Credentials (e.g. from Config):

    // Creates instance of service and sets credentials.
    Customer_Service service = new Customer_Service();
    service.UseDefaultCredentials = false;
    service.Credentials = new NetworkCredential("username", "password", "domain");
    

    More at MSDN: Walkthrough: Registering and Using a Page Web Service (SOAP)