具有相互SSL的自托管WCF服务(在服务和客户端之间)失败,403 Forbidden

时间:2014-04-20 00:34:01

标签: wcf ssl https mutual-authentication

我正在尝试在自托管 WCF服务和客户端应用程序(现在的命令提示符)之​​间设置Mutual SSL演示。最后,我试图找到一个解决方案,我在使用证书进行传入连接的服务器和多个客户端之间有传输安全性(不是消息安全性),每个客户端都有我可以使用的单独证书用于唯一标识每个客户端。

我已经尝试了许多不同的方法,但没有一个有效(我无法找到我一直在尝试做的确切示例)。每次我认为我越来越近,当我尝试调用服务时,我最终会在客户端出现异常。我遇到的最常见的例外是:

“The HTTP request was forbidden with client authentication scheme 'Anonymous'.”
Inner exception: "The remote server returned an error: (403) Forbidden."

有没有人对我可能做错了什么有任何想法,或者更好地了解如何在上述场景中设置相互SSL?

完全披露 - 截至目前,我在同一台计算机上运行客户端和服务器。不确定这是否重要。

下面的配置片段

服务和客户端代码相对微不足道,所以我非常有信心让他们开始工作。应用程序配置(特别是绑定和行为)和证书“更有趣”,所以我不那么自信。

我是如何创建证书的(逐字实际命令)

makecert -pe -n "CN=SelfSignedCA" -ss Root -sr LocalMachine  -a sha1 -sky signature -r -sv "SelfSignedCA.cer" "SelfSignedCA.pvk"
makecert -pe -n "CN=system" -ss my -sr LocalMachine -a sha1 -sky exchange -eku 1.3.6.1.5.5.7.3.1  -in "SelfSignedCA" -is Root -ir LocalMachine -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 Service.cer
makecert -pe -n "CN=client1" -ss my -sr LocalMachine -a sha1 -sky exchange -eku 1.3.6.1.5.5.7.3.1  -in "SelfSignedCA" -is Root -ir LocalMachine -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 Client1.cer

将证书与端口关联(逐字实际命令)

netsh http add urlacl url=https://+:44355/MyService/ user=EVERYONE

服务器设置

绑定:

  <wsHttpBinding>
    <binding name="CustomBinding">      
      <security mode="Transport">
        <transport clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>

行为:

    <serviceBehaviors>
      <behavior name="">
      <!--
      <serviceCredentials>
        <serviceCertificate
           findValue="system"
           storeLocation="LocalMachine"
           storeName="My"
           x509FindType="FindBySubjectName"/>
      </serviceCredentials>
      -->
      <serviceAuthorization
         serviceAuthorizationManagerType=
              "ClientAuthorization.ClientCertificateAuthorizationManager, Simulator.Service.SideA" />
    </behavior>
  </serviceBehaviors>

客户端

绑定:

  <wsHttpBinding>
    <binding name="CustomBinding">      
      <security mode="Transport">
        <transport clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>

行为

  <endpointBehaviors>
    <behavior name="ChannelManagerBehavior">
      <clientCredentials>
         <clientCertificate findValue="client1"
                           storeLocation="LocalMachine"
                           storeName="My"
                           x509FindType="FindBySubjectName" />
        <!--
        <serviceCertificate>
          <authentication certificateValidationMode="PeerOrChainTrust"/>
        </serviceCertificate>
        -->
      </clientCredentials>
     </behavior>
  </endpointBehaviors>

更新

因此,我向服务器添加了一个自定义用户名和密码验证程序,试图覆盖默认行为,并始终允许无论提供的凭据(我再也不想要用户名/密码验证)。 永远不会调用此验证程序。客户端仍然会获得&#34;身份验证方案&#39; Anonymous&#39;。“例外。

服务行为更新

  <serviceCredentials>
    <userNameAuthentication 
      userNamePasswordValidationMode="Custom"
      customUserNamePasswordValidatorType=
        "Service.ClientAuthorization.ClientUserNamePasswordValidatorManager, Service.SideA" />
  </serviceCredentials>

5 个答案:

答案 0 :(得分:3)

这是演示供您参考。我在win7 + vs2010 + client-server-on-same-machine下进行了测试。

服务器端:

[ServiceContract(Name="CalculatorService")]
    public interface ICalculatorService {
        [OperationContract]
        int Add(int x, int y);
    }

public class CalculatorService : ICalculatorService {
        public Int32 Add(Int32 x, Int32 y) {
            Console.WriteLine("{0}: service method called (x = {1}, y = {2})",
                Thread.CurrentThread.ManagedThreadId, x, y);
            return x + y;
        }
    }

class Program {
        static void Main(string[] args) {
            ServicePointManager.ServerCertificateValidationCallback +=
                (sender, certificate, chain, sslPolicyErrors) => true;

            using (var serviceHost = new ServiceHost(typeof(CalculatorService))) {
                serviceHost.Opened += delegate {
                    Console.WriteLine("{0}: service started", 
                        Thread.CurrentThread.ManagedThreadId);
                };
                serviceHost.Open();
                Console.Read();
            }
        }
    }

<?xml version="1.0" encoding="utf-8" ?> <configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="transportSecurity">
                    <security mode="Transport">
                        <transport clientCredentialType="Certificate"/>
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>

        <services>
            <service name="WcfService.CalculatorService">
                <endpoint address="https://hp-laptop:3721/calculatorservice"
                          binding="wsHttpBinding"
                          bindingConfiguration="transportSecurity"
                          contract="Contract.ICalculatorService" />
            </service>
        </services>
    </system.serviceModel> </configuration>

客户方:

class Program {
        static void Main(string[] args) {
            using (var channelFactory =
                new ChannelFactory<ICalculatorService>("calculatorservice")) {
                ICalculatorService proxy = channelFactory.CreateChannel();
                Console.WriteLine(proxy.Add(1, 2));
                Console.Read();
            }
            Console.Read();
        }
    }

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="transportSecurity">
                    <security mode="Transport">
                        <transport clientCredentialType="Certificate"/>
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <behaviors>
            <endpointBehaviors>
                <behavior name="defaultClientCertificate">
                    <clientCredentials>
                        <clientCertificate 
                            storeLocation="LocalMachine" 
                            storeName="My" 
                            x509FindType="FindBySubjectName" 
                            findValue="client1"/>
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <client>
            <endpoint name="calculatorservice" behaviorConfiguration="defaultClientCertificate"
                      address="https://hp-laptop:3721/calculatorservice"
                      binding="wsHttpBinding"
                      bindingConfiguration="transportSecurity"
                      contract="Contract.ICalculatorService"/>
        </client>
    </system.serviceModel>
</configuration>

证书创建:

自创CA

  

makecert -n&#34; CN = RootCA&#34; -r -sv c:\ rootca.pvk c:\ rootca.cer

创建后,将此证书导入“受信任的根证书”#39;通过证书控制台。这是阻止您提到的异常的步骤。

服务计划证书

  

makecert -n&#34; CN = hp-laptop&#34; -ic c:\ rootca.cer -iv c:\ rootca.pvk -sr   LocalMachine -ss My -pe -sky exchange

请注意,上面的CN值应与服务地址的DNS部分匹配。例如惠普笔记本电脑是我的电脑名称。服务端点的地址为&#34; https://google.com:/...&#34;。 (由于某些stackoverflow规则,将google dot com替换为&#39; hp-laptop&#39;)

使用服务程序注册服务证书:

  

netsh http add sslcert ipport = 0.0.0.0:3721   CERTHASH = 6c78ad6480d62f5f460f17f70ef9660076872326   APPID = {a0327398-4069-4d2d-83c0-a0d5e6cc71b5}

certhash值是服务程序证书的指纹(使用证书控制台检查)。 appid是来自服务程序文件的GUID&#34; AssemblyINfo.cs&#34;。

客户计划证书:

  

makecert -n&#34; CN = client1&#34; -ic c:\ rootca.cer -iv c:\ rootca.pvk -sr   LocalMachine -ss My -pe -sky exchange

更新:根据伤寒对此解决方案的经验,“匿名者”#39;由于该服务器中有太多受信任的root权限,因此仍然存在异常。伤寒提供了两个链接来解决这个问题。

http://support.microsoft.com/kb/2464556

http://blog.codit.eu/post/2013/04/03/Troubleshooting-SSL-client-certificate-issue-on-IIS.aspx

答案 1 :(得分:3)

另一种选择是monitor the actual exchange,包括ssl v3握手。我碰到了类似的东西,最终发现SSL Handshake失败了,因为我有太多可信任的证书颁发机构。要解决此问题,我需要关注this resolution。该解决方案使Windows的SSL部分不会将受信任的证书颁发机构发送回客户端;因此,允许握手完成。

答案 2 :(得分:2)

我有几天同样的问题,直到最后,我意识到这一行创建了“服务器身份验证”证书。您需要“客户端身份验证”证书,否则客户端证书将被视为无效。

  

makecert -pe -n“CN = client1”-ss my -sr LocalMachine -a sha1 -sky   exchange -eku 1.3.6.1.5.5.7.3.1 -in“SelfSignedCA”-is Root -ir   LocalMachine -sp“Microsoft RSA SChannel加密提供程序”-sy   12 Client1.cer

所以只需使用 2 代替-eku参数:1.3.6.1.5.5.7.3.2

答案 3 :(得分:0)

在任何HTTP流量通过之前,SSL握手必须成功完成。您收到HTTP响应的事实表明您的SSL设置正常运行。

403 Forbidden响应表示服务器设置为在提供资源/页面之前需要HTTP基本身份验证用户名和密码。您需要您的客户端在HTTP级别提供基本的身份验证用户名/密码,以及您在TLS / SSL级别上已经执行的操作,或者您需要设置服务器以允许在没有HTTP基本身份验证的情况下进行访问。

答案 4 :(得分:0)

我遇到了同样的问题(Forbidden 403),我的客户端尝试使用MutualAuthentication连接到服务器。确实,SSL层不是问题,同时服务器没有得到请求,因为它被传输层阻止了。 但是我在2012年的服务器面临着这个问题 - 因为2008年的工作会很好。 通过

在服务配置中启用serviceSecurityAudit
<serviceSecurityAudit auditLogLocation="Application"
            suppressAuditFailure="false" 
            serviceAuthorizationAuditLevel="SuccessOrFailure" 
            messageAuthenticationAuditLevel="SuccessOrFailure" />

将Windows事件日志应用程序端的错误/失败显示为消息身份验证和服务授权错误。

对我有用的解决方案是通过

为clientCertificate引入自定义证书验证器
   <clientCertificate>              <authentication certificateValidationMode="Custom" customCertificateValidatorType="App.Web.Framework.MyX509CertificateValidator, App.Vertical.Send"  />
   </clientCertificate>

这需要在程序集中实现证书验证方法。

如果需要,我也可以提供这些细节。

由于 --Kirti Kunal Shah