WCF消息安全模式下的验证问题

wcf服务端配置:

<system.serviceModel>

    <bindings>
      <wsHttpBinding>
        <binding name="EndpointBinding">
          <security mode="Message">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <services>
      <service name="WcfService.Service1" behaviorConfiguration="httpBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfService/Service1/"/>
          </baseAddresses>
        </host>
        
        <endpoint address="http://202.196.96.105:8888/Service1" binding="wsHttpBinding" contract="WcfService.IService1" bindingConfiguration="EndpointBinding">

          <!--<identity>
            <dns value="WCFTest"/>
          </identity>-->
          
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="httpBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
          
          <serviceCredentials>
            <!--指定验证方式为Custom,表示自定义,既然是自定义的,就要指出用哪个类进行用户名密码验证,这里指定了Server程序集中的Server.Validator类,注意这里类完整名称的写法-->
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService.Validator, WcfService" />
            <!--指定用于数据加密的证书,LocalMachine表示本地计算机,Root表示受信任根证书颁发机构,192.168.90.81是证书标题(因为做证书时没指定标题,所以使用者默认就是标题),FindBySubjectName表示按标题查找-->
            <serviceCertificate storeLocation="LocalMachine" storeName="Root" findValue="WCFTest" x509FindType="FindBySubjectName" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

 

客户端代码:

WSHttpBinding bind = new WSHttpBinding();
            EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity("WCFTest");
            Uri uri = new Uri("http://202.196.96.2:8888/Service1");
            EndpointAddress address = new EndpointAddress(uri, identity);
            //EndpointAddress address = new EndpointAddress("http://202.196.96.105:8888/Service1");
            ChannelFactory<IService1> cf = new ChannelFactory<IService1>(bind, address);
            cf.Credentials.UserName.UserName = "lr";
            cf.Credentials.UserName.Password = "liuran123-";
            bind.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
IService1 iservice = cf.CreateChannel(); string str = iservice.GetData("测试成功!"); Console.Write(str); Console.Read();

服务端服务能监听成功,但是在客户端调用时会抛出这个异常信息:

Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值