匿名用戶端的訊息安全
下面的方案示範通過 Windows Communication Foundation (WCF) 訊息安全進行保護的用戶端和服務。其設計目的是使用訊息安全而非傳輸安全,以便將來可支援更加豐富的基於聲明的模型。有關 將豐富聲明用於授權的更多資訊,請參見使用標識模型管理聲明和授權。
有關應用程式範例,請參見Message Security Anonymous。
| 特徵 |
說明 |
安全模式 |
訊息 |
互通性 |
僅 WCF |
身分識別驗證(伺服器) |
初始協商要求伺服器身分識別驗證,而不是用戶端驗證 |
身分識別驗證(用戶端) |
無 |
完整性 |
是,使用共用安全上下文 |
保密性 |
是,使用共用安全上下文 |
傳輸 |
HTTP |
服務
下面的代碼和配置應獨立運行。執行下列操作之一:
- 使用代碼(而不使用配置)建立獨立服務。
- 使用提供的配置建立服務,但不定義任何終結點。
代碼
下面的代碼示範如何建立使用訊息安全的服務端點。
配置
以下配置可代替代碼使用。服務行為元素用於指定用來向用戶端驗證服務身份的認證。服務元素必須使用 behaviorConfiguration 屬性指定行為。繫結元素指定用戶端憑據類型為 None,這將允許匿名用戶端使用該服務。
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="ServiceCredentialsBehavior"> <serviceCredentials> <serviceCertificate findValue="contoso.com" storeLocation="LocalMachine" storeName="My" /> </serviceCredentials> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="ServiceCredentialsBehavior" name="ServiceModel.Calculator"> <endpoint address="http://localhost/Calculator" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator" name="CalculatorService" contract="ServiceModel.ICalculator" /> </service> </services> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_ICalculator" > <security mode="Message"> <message clientCredentialType="None" /> </security> </binding> </wsHttpBinding> </bindings> <client /> </system.serviceModel></configuration>
用戶端
下面的代碼和配置應獨立運行。請執行下列操作之一:
- 使用代碼(和用戶端代碼)建立獨立用戶端。
- 建立不定義任何端點位址的用戶端。而使用將配置名稱作為參數的用戶端建構函式。例如:
代碼
下面的代碼建立用戶端的一個執行個體。綁定使用訊息模式安全,用戶端憑據類型設定為 None。
配置
下面的代碼將配置用戶端。
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_ICalculator" > <security mode="Message"> <message clientCredentialType="None" /> </security> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://machineName/Calculator" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator" contract="ICalculator" name="WSHttpBinding_ICalculator"> <identity> <dns value="contoso.com" /> </identity> </endpoint> </client> </system.serviceModel></configuration>