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

发布于 2024-12-29 02:51:40 字数 1503 浏览 0 评论 0原文

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

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

I want to be able to set the uri for a service endpoint in code while having the configuration for the security behaviour set in the config file.

The following gets me some of the way there, the service uses the correct binding configuration - but I cannot find a way to move the cert configuration into the config file.

Edit: note there was some confusion here - the config file configures the cert for Message level security and the ssl port controls the cert for Transport level - as per Richard Blewett's answer

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"
//    );

the commented out code is what I need to find some equivalent for in the config file

   <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>  

Edit: for posterity I have updated the question and answer to cover both message level and transport level as I need cater for both.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2025-01-05 02:51:40

对于消息安全性,此服务行为应该满足您的需要。

<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

For Message security this service behavior should give you what you need

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

However, you are using transport security - in other words HTTPS with wsHttpBinding. Therefore the cert is defined by the configuration of http.sys where you bind a cert to a port. On Windows 2008 you use netsh.exe to contorl and view this configuration. on Windows 2003 you use the much less usable tool httpcfg.exe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文