在配置文件中为具有在代码中创建的端点的服务设置wcf服务凭据

时间:2012-01-25 17:12:29

标签: wcf wcf-binding wcf-endpoint wcf-behaviour

我希望能够在代码中为服务端点设置uri,同时配置配置文件中设置的安全行为。

下面给我一些方法,该服务使用正确的绑定配置 - 但我找不到将证书配置移动到配置文件的方法。

编辑:请注意,这里有一些混淆 - 配置文件为消息级别安全配置证书,ssl端口控制传输级别的证书 - 根据Richard Blewett的回答

var svc = new ServiceHost( typeof (MyService), new Uri(s));
svc.Authorization.PrincipalPermissionMode = 
                  PrincipalPermissionMode.UseWindowsGroups;
svc.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding("MyBinding"), "");
//svc.Credentials.ServiceCertificate.SetCertificate(
//    StoreLocation.LocalMachine,
//    StoreName.My,
//    X509FindType.FindBySubjectName,
//    "mycertname"
//    );

注释掉的代码是我需要在配置文件中找到一些等效的代码

   <system.serviceModel>
     <services>
       <service name="MyNamespace.MyService" behaviorConfiguration="MyBehavior">
       </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="MyBinding">
          <security mode="Transport">
            <transport clientCredentialType="Windows"/>
          </security>
          <!-- Or for message level security
          <security mode="Message">
            <message clientCredentialType="Certificate"/>
          </security>
          -->
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>  

编辑:对于后代,我更新了问题和答案,以涵盖消息级别和传输级别,因为我需要满足这两个要求。

1 个答案:

答案 0 :(得分:0)

对于Message安全性,此服务行为应该为您提供所需的

<behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceCredentials>
            <serviceCertificate findValue="mycertname"
                                x509FindType="FindBySubjectName"
                                storeLocation="LocalMachine"
                                storeName="My"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
</behaviors>

但是,您使用的是传输安全性 - 换言之,使用wsHttpBinding的HTTPS。因此,证书由http.sys的配置定义,您将证书绑定到端口。在Windows 2008上,使用netsh.exe来控制和查看此配置。在Windows 2003上,您使用功能较少的工具httpcfg.exe

相关问题