1

我希望能够在代码中设置服务端点的 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>  

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

4

1 回答 1

1

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

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

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

于 2012-01-25T17:17:28.507 回答