将clientCredentialType更改为UserName,但使用x509证书

时间:2011-06-29 14:29:15

标签: wcf service client certificate

我已经制作了一个wcf应用程序和一个客户端。 WCF应用。必须知道哪些用户和密码访问了服务操作。这就是我所做的:
服务器网络配置:

    <?xml version="1.0"?>
     <configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="wsHttpEndpointBinding">
                    <security>
                        <message clientCredentialType="Certificate" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="Auth">
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceCredentials>
                        <clientCertificate>
                            <authentication certificateValidationMode="PeerTrust"/>
                        </clientCertificate>
                        <serviceCertificate findValue="WCfServer"
                        storeLocation="CurrentUser"
                        storeName="My"
                        x509FindType="FindBySubjectName" />
                    </serviceCredentials>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="Auth" name="Service">
                <endpoint address="" binding="wsHttpBinding"       bindingConfiguration="wsHttpEndpointBinding" contract="IService"/>
            </service>
        </services>
    </system.serviceModel>
    <system.web>
        <compilation debug="true"/>
    </system.web>
</configuration>

客户端配置:

     <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
    <system.serviceModel>
        <bindings>
   <wsHttpBinding>
    <binding name="WSHttpBinding_IService" closeTimeout="00:01:00"
     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
     maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
     textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
      maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     <reliableSession ordered="true" inactivityTimeout="00:10:00"
      enabled="false" />
     <security mode="Message">
      <transport clientCredentialType="Windows" proxyCredentialType="None"
       realm="" />
      <message clientCredentialType="Certificate" negotiateServiceCredential="true" />
     </security>
    </binding>
   </wsHttpBinding>
  </bindings>
        <behaviors>
            <endpointBehaviors>
                <behavior name="CustomBehavior">
                    <clientCredentials>
                        <clientCertificate findValue="WcfClient" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="My" />
                        <serviceCertificate>
                            <authentication certificateValidationMode="PeerTrust"/>
                        </serviceCertificate>
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <client>
            <endpoint address="http://localhost:30341/WCFAuthTest/Service.svc"
             binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
             contract="Service.IService" name="WSHttpBinding_IService" behaviorConfiguration="CustomBehavior">
                <identity>
                    <dns value="WcfServer" />
                </identity>
            </endpoint>
  </client>
    </system.serviceModel>
</configuration>

我如何生成证书: http://www.codeproject.com/KB/WCF/9StepsWCF.aspx
服务运营:

    public string TestAccess()
{
    return OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
}

客户端:

            ServiceClient client = new ServiceClient();
        client.ClientCredentials.UserName.UserName = "Admin";
        client.ClientCredentials.UserName.Password = "123";
        Console.WriteLine(client.TestAccess());
        Console.ReadLine();

程序必须返回Admin但不会: http://img27.imageshack.us/img27/3104/returnz.png
我知道我必须将clientCredentialType更改为UserName,但它会给我一个错误

1 个答案:

答案 0 :(得分:1)

如果要传递用户名和密码,则必须将客户端凭据类型设置为UserName。将其设置为证书是为了使用客户端证书。这是一些how to article