C# 使用X509证书时如何配置WCF客户端绑定?
我正在尝试在我的WCF客户端中设置凭据,如下所示C# 使用X509证书时如何配置WCF客户端绑定?,c#,wcf,C#,Wcf,我正在尝试在我的WCF客户端中设置凭据,如下所示 ClientCredentials loginCredentials = new ClientCredentials(); loginCredentials.UserName.UserName = this.UserId; loginCredentials.UserName.Password = this.Password; loginCredenti
ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = this.UserId;
loginCredentials.UserName.Password = this.Password;
loginCredentials.ClientCertificate.Certificate = new X509Certificate2(this.Certificate);
var defaultCredentials = channelFactory.Endpoint.Behaviors.Find<ClientCredentials>();
channelFactory.Endpoint.Behaviors.Remove(defaultCredentials);
channelFactory.Endpoint.Behaviors.Add(loginCredentials);
ClientCredentials loginCredentials=newclientcredentials();
loginCredentials.UserName.UserName=this.UserId;
loginCredentials.UserName.Password=此.Password;
loginCredentials.ClientCertificate.Certificate=新的X509Certificate2(this.Certificate);
var defaultCredentials=channelFactory.Endpoint.Behaviors.Find();
channelFactory.Endpoint.Behaviors.Remove(defaultCredentials);
channelFactory.Endpoint.Behaviors.Add(loginCredentials);
我有如下wcf客户端绑定设置
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="PPWSApiOrderBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://ws.test.globalgateway.com:443/wsapi/services"
binding="basicHttpBinding" bindingConfiguration="PPWSApiOrderBinding"
contract="PPWebService.PPWSApiOrder" name="PPWSApiOrderPort" />
</client>
</system.serviceModel>
我收到一个错误“http请求未经授权,客户端身份验证方案为“匿名”。从服务器收到的身份验证标头为“基本领域=…”
远程服务器返回错误:(401)未经授权
感谢您的帮助。我不确定在app.config文件的binding部分需要更改什么。谢谢 看起来您的错误可能在传输节点中 查看此MSDN页面以获取参考 这里有一个例子
<wsHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport" />
<transport clientCredentialType = "Windows" />
</security>
</binding>
</wsHttpBinding >
或
谢谢您的回复。如果需要使用clientCredentialType=Certificate,是否始终必须使用消息安全性?现在,在将其更改为消息安全性后,我收到一个不同的错误。错误为“提供的URI方案“https”无效;应为“http”。参数名称:via”。我不确定这是否意味着我之前的错误进一步恶化了,或者这是否与消息安全有关?有什么想法吗?谢谢。我相信是这样的,因为它的名字本身就表明它在寻找证书。。如果没有,请尝试clientCredentialType=“Window”不确定您的安全设置或需要如何,也不确定开发人员对http绑定的期望是什么
<wsHttpBinding>
<binding name="MessageSecurity">
<security mode="Message" />
<message clientCredentialType = "Certificate" />
</security>
</binding>
</wsHttpBinding >