简体   繁体   English

使用用户名和密码的WCF服务

[英]WCF Service with Username and password

A little new with WCF services when using username and passwords. 使用用户名和密码时,WCF服务有一些新功能。 I followed the tutorial found at http://www.codeproject.com/Articles/96028/WCF-Service-with-custom-username-password-authenti in order to protect my web service with a username and password. 我遵循了http://www.codeproject.com/Articles/96028/WCF-Service-with-custom-username-password-authenti上的教程,以使用用户名和密码保护我的Web服务。

My Config file is below 我的配置文件在下面

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

Now i can view the WDSL in my browser and i know the certificate works as expected locally. 现在,我可以在浏览器中查看WDSL,并且我知道证书可以在本地正常工作。 When i connect to the service using the WCF test tool it doesnt prompt me for a username and password. 当我使用WCF测试工具连接到服务时,它不会提示我输入用户名和密码。

According to the link i posted and following ive not even done the final step (adding the code to pass in the username and password) but i can still connect to the service and retrieve all the data. 根据我发布的链接,在ive之后,我什至没有完成最后一步(添加代码以传递用户名和密码),但是我仍然可以连接到该服务并检索所有数据。

What have i missed out and how could i restrict the service where only a username and password allows the user/service to retrieve the data? 我错过了什么?在只有用户名和密码才能允许用户/服务检索数据的情况下,如何限制服务?

Edit 1: 编辑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>

Based on a quick review of the linked CodeProject page, your config file seems to be a bit off (as it does not indicate that any endpoints are actually using a client credential type). 根据对链接的CodeProject页面的快速查看,您的配置文件似乎有点不对(因为它并不表示任何端点实际上都在使用客户端凭据类型)。

The "NewBinding0" specifies clientCredentialType="Certificate" but the article indicates the value should be: “ NewBinding0”指定clientCredentialType =“ Certificate”,但文章指出该值应为:

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

Also, the Service definition only defines a “mex” (metadata) endpoint. 此外,服务定义仅定义“ mex”(元数据)端点。 You most likely will want to define an wsHttpBinding.. endpoint, which utilizes the corrected binding that specifies clientCredentialType="UserName". 您很可能希望定义一个wsHttpBinding ..端点,该端点利用指定了clientCredentialType =“ UserName”的更正绑定。

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

Hope this helps. 希望这可以帮助。
Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM