簡體   English   中英

使用WCF Web服務時如何對客戶端進行身份驗證?

[英]How to authenticate client while consuming the WCF web service?

我想在使用Web服務時對客戶端進行身份驗證。 我看到客戶端公開了一個名為ClientCredential的屬性,我們可以在其中傳遞用戶名和密碼。 如何將這些信息傳遞給WCF Web服務,以及如何驗證用戶ID和密碼?

如果要使用帶有用戶名/密碼的ClientCredential,則需要像這樣在客戶端app.config中進行配置-使用傳輸安全性或郵件安全性,以適合您的方式使用,然后指定

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="UserNameSecurity">
          <security mode="Message">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </basicHttpBinding>

然后您需要在客戶端的端點上使用此綁定配置“ UserNameSecurity”:

    <client>
      <endpoint address="http://localhost:8888/MyService"
                binding="basicHttpBinding" bindingConfiguration="UserNameSecurity"
                contract="IMyService" />

在服務器端,您需要定義如何對用戶進行身份驗證-使用Windows(Active Directory域)或使用ASP.NET成員資格提供程序(及其關聯的用戶數據庫):

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Default">
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="MembershipProvider"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>

在這種情況下,將根據ASP.NET成員資格數據庫檢查您的用戶名/密碼。

如果所有這些都在公司內部的Intranet上,那么我寧願使用集成的Windows安全性-它更容易設置和使用,並且更加可靠和安全。 但是它僅在公司內部,公司防火牆內部起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM