我创建了一个C#控制台应用程序,右键单击解决方案资源管理器中的“引用”图标,然后选择“添加服务引用”。输入地址并单击“转到”时,出现以下错误:
Metadata contains a reference that cannot be resolved: 'https://MyWebAddress.com/MyService'.
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.
The remote server returned an error: (401) Unauthorized.
If the service is defined in the current solution, try building the solution and adding the service reference again.
该Web服务应该使用基于证书的身份验证。我已将客户证书加载到我的个人密钥存储中。
当我进入“高级”>“添加Web引用”,然后输入URL时,系统提示我选择证书。选择证书后,出现另一个错误:
Value cannot be null.
Parameter name: discoveryError & mexError
在服务器管理员将安全性更改为证书身份验证之前,我能够使用用户名和密码进行连接。因此,在安全性更改发生之前,我确实已经建立了服务参考。但是,当我尝试将该C#控制台应用程序连接到Web服务时,出现以下错误:
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.
这是我的应用程序配置中的XML:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBindingConfig" closeTimeout="00:01:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="655360000" maxBufferPoolSize="52428800" maxReceivedMessageSize="655360000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384000" maxBytesPerRead="409600000" maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="Certificate" proxyCredentialType="None" realm="" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://MyWebAddress.com/MyService" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfig" contract="MyService" name="MyServiceTest" />
</client>
</system.serviceModel>
这是我将证书分配给代理的方式。 GetCertificate函数来自我公司的系统管理员制作的专有DLL(Web服务来自其他公司)。
将证书添加到代理:
[System.Security.SecuritySafeCritical]
protected void Initialize(string endpointName)
{
if (this.ClientCredentials != null)
{
var cert = MyCompany.CryptoHelper.GetCertificate(Settings.LocalCert_FriendlyName);
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;
}
}
当我使用Internet Explorer连接到Web服务时,提示输入证书,选择它,然后出现以下错误:
401 / No+client+certificate+chain+in+this+request