且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 linux 机器上运行 c# 应用程序时,SOAP 身份验证失败

更新时间:2023-02-25 21:50:34

.Net Core SOAP 客户端,带有 NTLM 身份验证和 CredentialCache

MSDN 所述和这里

BasicHttpBinding basicHttpBinding = new BasicHttpBinding();

basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

EndpointAddress endpoint = new EndpointAddress("http://myservice");

var factory = new ChannelFactory<IMyService>(basicHttpBinding, endpoint);
CredentialCache myCredentialCache = new CredentialCache();

NetworkCredential myCreds = new NetworkCredential("username", "password", "domain");
myCredentialCache.Add("ContoscoMail", 45, "NTLM", myCreds);
factory.Credentials.Windows.ClientCredential = 
         myCredentialCache.GetCredential("ContosoMail", 45, "NTLM");

var client = factory.CreateChannel(); 

// ... use the webservice

更新:这是 2.1 中修复的错误

正如在此处遇到的那样,并已作为错误修复此处,它应该适用于 .net core 2.1(未发布并计划用于 2018 年第一季度).所以现在,当从 Linux 连接时,您应该尝试使用另一种类型的身份验证(查看 RuntimeInformation.IsOSPlatform).

Update: it's a bug fixed in 2.1

As already encountered here and fixed as a bug here, it should work with .net core 2.1 (not released and scheduled for Q1 2018). So right now, you should try to use another type of authentication when connecting from Linux (look at RuntimeInformation.IsOSPlatform).