2012-01-29 66 views
1

我创建了一个wcf服务,托管它在IIS(7.5),它工作正常。我现在想添加用户名认证,并遇到一些问题。 配置文件是这样的:WCF问题 - wsHttpBinding与用户名认证和TransportWithMessageCredentials

<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. 1)makecert -n “CN = RootCATest” -r -sv RootCATest。 PVK RootCATest.cer

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

  3. 3)makecert -sk CertTest -iv RootCATest.pvk -n “CN =虚假” -ic RootCATest.cer - SR LOCALMACHINE -ss我-sky交换-pe

在IIS我增加了对HTTPS,并在服务器证书我有这个结合: enter image description here

当我运行SvcUtil工具https://localhost/WarcraftServiceSite/WarService.svc我得到这个异常: "There was an error downloading https://localhost/WarcraftServiceSite/WarService.svc. The underlying connection was closed.Could not establish trust relationship for the SSL/TLS secure channel. The remote certificate is invalid according to the validation procedure."

后来编辑:看来,叫SvcUtil工具的正确方法是使用HTTP没有使用https,即使我有这个<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />

回答

2

因为这只是一个测试证书,您可以将以下内容添加到客户端以供其使用。当你从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的证书只能用于测试。