【问题标题】:WCF Service with Username and password带有用户名和密码的 WCF 服务
【发布时间】:2013-10-14 19:20:54
【问题描述】:

使用用户名和密码时 WCF 服务的一些新功能。我按照http://www.codeproject.com/Articles/96028/WCF-Service-with-custom-username-password-authenti 上的教程进行操作,以便使用用户名和密码保护我的 Web 服务。

我的配置文件在下面

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

  <system.web>
    <compilation debug="false" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="NewBehavior0" name="TService">
        <endpoint address="mex" binding="mexHttpBinding" contract="ITechnology" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="NewBinding0">
          <security>
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior0">
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="PeerTrust" />
            </clientCertificate>
            <serviceCertificate findValue="Server" storeLocation="CurrentUser"
          storeName="TrustedPeople" x509FindType="FindBySubjectName" />
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="TService, Services1"/>
          </serviceCredentials>
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

现在我可以在浏览器中查看 WDSL,并且我知道证书在本地按预期工作。当我使用 WCF 测试工具连接到服务时,它不会提示我输入用户名和密码。

根据我发布的链接,我什至没有完成最后一步(添加代码以传递用户名和密码),但我仍然可以连接到服务并检索所有数据。

我错过了什么,我该如何限制只有用户名和密码允许用户/服务检索数据的服务?

编辑 1:

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="NewBehavior0" name="TechService">
        <endpoint address="mex" binding="mexHttpBinding" contract="ITechService" />
       <endpoint address="TechService.svc" binding="wsHttpBinding" bindingConfiguration="" contract="ITechService" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="NewBinding0">
          <security>
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior0">
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="PeerTrust" />
            </clientCertificate>
            <serviceCertificate findValue="Server" storeLocation="CurrentUser"
              storeName="TrustedPeople" x509FindType="FindBySubjectName" />
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="TechService, Services1"/>
          </serviceCredentials>
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

【问题讨论】:

  • 您的服务元素和端点元素的名称和合同属性应该是完全限定的名称,即命名空间.TService 和命名空间.ITechnology。根据您的配置,您的端点没有使用正确的绑定机制。它应该是具有传输安全性的 wsHttpBinding 或 basicHttpBinding

标签: .net wcf web-services wcf-security


【解决方案1】:

根据对链接的 CodeProject 页面的快速查看,您的配置文件似乎有点偏离(因为它并不表示任何端点实际上正在使用客户端凭据类型)。

“NewBinding0”指定 clientCredentialType="Certificate" 但文章指出该值应为:

<binding name="NewBinding0">
    <security mode="Message">
        <message clientCredentialType="UserName"/>
    </security>
</binding>

此外,服务定义仅定义“mex”(元数据)端点。您很可能希望定义一个 wsHttpBinding.. 端点,该端点使用指定 clientCredentialType="UserName" 的更正绑定。

<endpoint address="" binding="wsHttpBinding" bindingConfiguration="NewBinding0"/>

希望这会有所帮助。
问候,

【讨论】:

  • 感谢我编辑了我的原始帖子,并在编辑 1 下包含了最新的配置文件,但它仍然无法按预期工作。请注意,当我连接到服务时,我会在连接到服务之前提示我输入用户名/密码或类似信息?
  • 基于Edit 1的配置文件,bindingConfiguration好像还是没有设置(bindingConfiguration="")。您需要设置 bindingConfiguration="NewBinding0" 才能应用绑定安全模式。
猜你喜欢
  • 2017-01-31
  • 1970-01-01
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
相关资源
最近更新 更多