在Net.Core 2.1中使用SOAP WCF服务时,通过协商而不是NTLM进行身份验证

时间:2019-09-19 16:33:21

标签: .net .net-core soap-client ntlm ntlm-authentication

我试图使用NTLM身份验证方案连接到WCF SOAP服务,使用.Net Framework 4.x时我成功了,但是当我尝试在.NET Core 2.1中使用相同的服务时,它失败了(因为身份验证尽管我在代码中设置了Ntlm,但该方案仍以协商方式发送),我正在使用提琴手,并附上了一些图片。

Net Framework 4.x's Header Net Framework 4.x的标题

Net Core 2.1's Headers

Net Core 2.1的标题

这是我正在使用.NetCore2.1和.NetFramework 4.5.x的源代码(可以正常工作):

System.ServiceModel.Channels.CustomBinding binding = new System.ServiceModel.Channels.CustomBinding();
System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
binding.Elements.Add(textBindingElement);
System.ServiceModel.Channels.HttpsTransportBindingElement httpsBindingElement = new System.ServiceModel.Channels.HttpsTransportBindingElement();
httpsBindingElement.AllowCookies = true;
httpsBindingElement.MaxBufferSize = int.MaxValue;
httpsBindingElement.MaxReceivedMessageSize = int.MaxValue;
httpsBindingElement.AuthenticationScheme = System.Net.AuthenticationSchemes.Ntlm;
binding.Elements.Add(httpsBindingElement);
return binding;

_client.ClientCredentials.Windows.ClientCredential.Domain = "domain";
_client.ClientCredentials.Windows.ClientCredential.UserName = "username";
_client.ClientCredentials.Windows.ClientCredential.Password = "password";
_client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

.NetCore2.1中出现的错误是:

  

发生一个或多个错误。 (HTTP请求未经授权   客户端身份验证方案“ Ntlm”。认证头   从服务器收到的是“ NTLM,协商”。)

任何帮助将不胜感激,谢谢!。

1 个答案:

答案 0 :(得分:0)

请检查this thread。线程包含几种解决方法:

// https://github.com/dotnet/wcf/issues/2923#issuecomment-408341667
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

// https://github.com/dotnet/wcf/issues/2923
ConnectionClose = false
ExpectContinue = false
相关问题