Search code examples
c#wcf.net-corereporting-services

SSRS report with .NET Core, The HTTP request is unauthorized with client authentication scheme 'Ntlm'


I'm trying to generate an SSRS report from .NET Core application and I'm following this tutorial
But I'm getting the following exception

The HTTP request is unauthorized with client authentication scheme 'Ntlm'. The authentication header received from the server was 'NTLM'.

When I remove the credentials and use BasicHttpSecurityMode.None with HttpClientCredentialType.None everything is working fine but I need to add credentials to the service

I know there are many answers to this error but I've tried almost all of them but nothing is working for me

Here is what I've tried:

I tried to add ProxyCredentialType to be Ntlm but I got the same error

var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Ntlm;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
binding.MaxReceivedMessageSize = 10485760; //10MB limit
var rsExec = new ReportExecutionServiceSoapClient(binding, new EndpointAddress(SSRSReportExecutionUrl));
var clientCredentials = new NetworkCredential(SSRSUsername, SSRSPassword, ".");
if (rsExec.ClientCredentials != null)
            {
                rsExec.ClientCredentials.Windows.AllowedImpersonationLevel =System.Security.Principal.TokenImpersonationLevel.Impersonation;
                rsExec.ClientCredentials.Windows.ClientCredential = clientCredentials;
            }

Also I tried to replace HttpClientCredentialType.Ntlm with HttpClientCredentialType.Windows but I got this error

System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'NTLM'

Some answers suggest modifying web.config file or using Microsoft Service Configuration Editor to edit it but I don't found this file or this editor in my case, I think these exist in .NET framework, not .NET core but I'm not sure

Any idea what should I do to fix this error?


Solution

  • This is what worked for me, I removed binding.Security.Transport.ProxyCredentialType line and added binding.UseDefaultWebProxy to be true;

    BasicHttpBinding binding = new BasicHttpBinding();
    binding.MaxBufferPoolSize = 0;
    binding.MaxReceivedMessageSize = 5242880;
    binding.TextEncoding = System.Text.Encoding.UTF8;
    binding.TransferMode = TransferMode.Buffered;
    binding.UseDefaultWebProxy = true;
    
    binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
    
    var rsExec = new ReportExecutionServiceSoapClient(binding, new EndpointAddress(SSRSReportExecutionUrl));
    rsExec.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
    rsExec.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;