且构网

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

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

更新时间:2023-02-25 21:54:55

.Net Core SOAP客户端NTLM身份验证和凭据缓存



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(未发布且未计划在 Q1 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).