我无法从在 linux 机器(docker 容器)上运行的 .NET Core 成功调用具有 NTLM 身份验证的 WCF 服务。 不过,相同的代码在 Windows 10 上也能完美运行。
我做了什么:
- 将此添加到
ConfigureServices
:
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
- 运行
apt-get -y install gss-ntlmssp
- 这是调用服务之前的代码:
var client = new WcfServiceSoapClient();
client.Endpoint.Address = new EndpointAddress(settings.Uri);
client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential
{
Domain = settings.Domain,
UserName = settings.Username,
Password = settings.Password
};
var binding = (BasicHttpBinding)client.Endpoint.Binding;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Ntlm;
如前所述,这在 Windows 10 上运行良好。在 Linux 上,会记录以下错误:
System.ServiceModel.Security.MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Ntlm'. The authentication header received from the server was 'NTLM, Negotiate'.
at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass1_0.<CreateGenericTask>b__0(IAsyncResult asyncResult)
问题是:为什么它在 linux 上仍然失败?
最佳答案
在 net core GitHub 上的许多人的帮助下,我遇到了同样的问题并且能够解决它。
本质上net core使用底层操作系统来处理http调用。之前,在 Linux 上使用 libCurl,但较新的 SocketsHttpHandler 使用 gss,它不支持 ntlm(至少在“microsoft/dotnet:2.2-aspnetcore-runtime-stretch-slim”图像中没有)。
要解决此问题,您需要使用 docker 文件在容器中安装一个额外的库。紧接着
来自微软/dotnet:2.2-aspnetcore-runtime-stretch-slim
(或者你使用的任何图片)
在下一行添加以下内容:
运行 apt-get update && apt-get install -y --no-install-recommends apt-utils gss-ntlmssp
希望这对您有所帮助!
关于c# - 在 Linux 上从 .NET Core 使用 NTLM 身份验证调用 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150279/