'Windows Authentication not working in WCF REST service from a Power BI request

I'm trying to access a REST service in a WCF server from power BI using Windows Authentication.

Currently the web request from Power BI is done to a nodejs server where we can get the NTLM authentication data in the 'authorization' header of the request.

But I need to do the same request on a WCF server instead. The Rest service of the WCF is working well, when not using any authentication I can access it with the power BI request through https without any issue.

But when I activate the authentication in the WCF server (with binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows), the request is rejected because the authentication fails. I cannot even use the NTLM data as the 'authentication' field in header is not in the received request (when authentication is not set, but I assume that's normal).

For now, everything is running on my machine, and I'm using the "Use my current credentials" option when doing the Windows authentication in Power BI. OF course there's an Active Directory.

the code in the WCF server:

private void StartRestServiceHosts(int port)
        {
            try
            {
                using (ServerContainerScope containerScope = new ServerContainerScope())
                {
                    RequestContext.Current.Initialize(LogAreas.Server, Shared.MainUserLogin);
                    string protocol = Shared.HttpsEnabled ? "https" : "http";
                    string uri = string.Format("{0}://{1}:{2}/Rest/", protocol, System.Environment.MachineName, port);
                    Uri httpBaseAddress = new Uri(uri);

                    var defaultWebHttpBehavior = new WebHttpBehavior()
                    {
                        AutomaticFormatSelectionEnabled = true,
                        DefaultBodyStyle = WebMessageBodyStyle.Wrapped,
                        DefaultOutgoingRequestFormat = WebMessageFormat.Json,
                        DefaultOutgoingResponseFormat = WebMessageFormat.Json,
                        HelpEnabled = false
                    };

                    foreach (ServiceDefinition serviceDefinition in _registeredRestServices.Values)
                    {
                        string currentServiceName = serviceDefinition.Name;

                        if (!_restServiceHosts.ContainsKey(currentServiceName))
                        {
                            ServiceHost host = new ServiceHost(serviceDefinition.Type,
                                new Uri(httpBaseAddress, serviceDefinition.Type.Name));


                            host.Authorization.ServiceAuthorizationManager = new PublicAuthorization();

                            Type contract = serviceDefinition.Type.GetInterface("I" + serviceDefinition.Type.Name);
                            ServiceEndpoint endPoint = new ServiceEndpoint(
                                ContractDescription.GetContract(contract),
                                _restBinding,
                                new EndpointAddress("{0}{1}".FormatWith(uri, contract.Name))
                            );

                            endPoint.Behaviors.Add(defaultWebHttpBehavior);
                            endPoint.Behaviors.Add(new CorsSupportBehavior());

                            host.AddServiceEndpoint(endPoint);

                            _restServiceHosts.Add(currentServiceName, host);
                        }

                        // Open 
                        if (_restServiceHosts[currentServiceName].State != CommunicationState.Opened)
                        {
                            _restServiceHosts[currentServiceName].Open();
                        }
                    }

                    OnWcfRestServicesStarted?.Invoke(null, true);
                }
            }
            catch (Exception ex)
            {
                OnWcfRestServicesStarted?.Invoke(null, false);
            }
        }

public WebHttpBinding CreateWebHttpBinding(string name)
    {
        WebHttpBinding binding = new WebHttpBinding();
        binding.Security.Mode = Shared.HttpsEnabled ? WebHttpSecurityMode.Transport : WebHttpSecurityMode.None;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
        //binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
        binding.Name = name;
        binding.ReceiveTimeout = new TimeSpan(1, 0, 0);
        binding.MaxBufferSize = 2147483647;
        binding.MaxReceivedMessageSize = 2147483647;

        return binding;
    }

  <appSettings>
    <add key="HttpsEnabled" value="true"/>
  </appSettings>

Any ideas as to why the authentication is not working? Thanks in advance!!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source