.net WCF问题-具有用户名身份验证和TransportWithMessageCredentials的wsHttpBinding

.net WCF问题-具有用户名身份验证和TransportWithMessageCredentials的wsHttpBinding,.net,wcf,.net,Wcf,我创建了一个wcf服务,将其托管在IIS(7.5)中,运行良好。我现在想添加用户名身份验证,但遇到了一些问题。 配置文件如下所示: <system.serviceModel> <services> <service behaviorConfiguration="warServBehavior" name="WcfServiceLibrary.WarcraftService"> <endpoint address="

我创建了一个wcf服务,将其托管在IIS(7.5)中,运行良好。我现在想添加用户名身份验证,但遇到了一些问题。 配置文件如下所示:

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="warServBehavior" name="WcfServiceLibrary.WarcraftService">
        <endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary.IWarcraftService" bindingConfiguration="warWsHttpBinding" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="warWsHttpBinding">
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="None"/>
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="warServBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="BogusValidator, App_Code"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

关于证书,我做了以下工作(受msdn启发):

  • 1) makecert-n“CN=RootCATest”-r-sv RootCATest.pvk RootCATest.cer

  • 2) 已将其添加到受信任的根证书颁发机构

  • 3) makecert-sk CertTest-iv RootCATest.pvk-n“CN=false”-ic RootCATest.cer-sr localmachine-ss my-sky exchange-pe
  • 在IIS中,我添加了https绑定,在服务器证书中,我有以下内容:

    当我运行svcutil时,出现以下异常:
    “下载时出错https://localhost/WarcraftServiceSite/WarService.svc. 基础连接已关闭。无法为SSL/TLS安全通道建立信任关系。根据验证过程,远程证书无效。“


    稍后编辑:调用svcutil的正确方法似乎是使用http而不是https,尽管我有这个

    ,因为这只是一个测试证书,您可以将以下内容添加到客户端,使其正常工作。当你从verisign等公司获得生产证书时,你就不需要这个了。 参考并添加以下各项的用法-System.Net、System.Net.Security、System.Security.Cryptography.X509Certificates

    使用ServicePointManager类并向ServerCertificateValidationCallback添加处理程序

      ServicePointManager.ServerCertificateValidationCallback
                   += RemoteCertificateValidate;
    
    然后处理程序impl

    private static bool RemoteCertificateValidate(
       object sender, X509Certificate cert, 
        X509Chain chain, SslPolicyErrors error)
    {
        // trust any certificate
        return true;
    }
    
    在使用代理之前,将处理程序连接到某个位置。 请记住,此代码和makecert的证书仅用于测试