如何保護WCF服務元數據
WCF服務元數據的保護方法是一個比較復雜的步驟。相信對于初學者來說,很難操作這一方法。在這里我們就為大家介紹一下相關方法技巧。#t#
概述
WCF是 Microsoft 為構建面向服務的應用程序而提供的統一編程模型(摘自MSDN),在分布式環境下的安全問題尤為重要,如果你覺得使用了WCF默認的安全措施可以讓你高枕無憂,那明天你可就以回家種田了,當然,對于學習來說,足夠了~,但我們講的是真正的項目應用,WCF在各種協議下的安全提供和保證是不盡相同的。
背景
某天,經理老陳對程序員小李說:小李,我們公司外包到一個項目,但是客戶要求采用分布式部署,現在項目快接近尾聲了,由于我們采用的是WCF,所以在部署的時候出現了一點問題,我們的服務好像誰都能訪問得到啊,這是為什么呢?
WCF服務元數據問題呈現
小李***件事就是去查看了服務配置文件,真的是不看不知道,一看嚇一跳,原來開發WCF時,采用的都是默認的配置,全是自動生成的代碼,沒有經過任何的改動,一想到項目將會以這種姿態交付,小李著實捏了一把汗。
- < services>
- < service name="WcfServiceLibrary2.Service1"
behaviorConfiguration="WcfService
Library2.Service1Behavior">- < host>
- < baseAddresses>
- < add baseAddress = "http://
localhost:8731/Design_Time_Addresses/
WcfServiceLibrary2/Service1/" />- < /baseAddresses>
- < /host>
- < endpoint address ="" binding=
"wsHttpBinding" contract="WcfService
Library2.IService1">- < identity>
- < dns value="localhost"/>
- < /identity>
- < /endpoint>
- < endpoint address="mex" binding=
"mexHttpBinding" contract="IMetadataExchange"/>- < /service>
- < /services>
- < behaviors>
- < serviceBehaviors>
- < behavior name="WcfServiceLibrary2
.Service1Behavior">- < serviceMetadata httpGetEnabled="True"/>
- < serviceDebug includeException
DetailInFaults="False" />- < /behavior>
- < /serviceBehaviors>
- < /behaviors>
WCF服務元數據解決之道
小李***件事就是把配置文件給修改好了,接著解決了困擾老陳許久的問題。
1、刪除元數據交換終結點信息
- < endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
2、將http協議獲取元數據重置為:false
- < serviceMetadata
httpGetEnabled="false"/>
3、一般我們都會在開發時配置為WCF服務元數據可發現,但是切記,發布你的服務前,一定要刪除了,目前,服務在一定范圍上得到了保護
4、最終配置如下
- < services>
- < service
- name="WcfServiceLibrary2.Service1"
- behaviorConfiguration=
"WcfServiceLibrary2.Service1Behavior">- < host>
- < baseAddresses>
- < add baseAddress = "http://
localhost:8731/Design_Time_Addresses
/WcfServiceLibrary2/Service1/" />- < /baseAddresses>
- < /host>
- < endpoint
- address =""
- binding="wsHttpBinding"
- contract="WcfServiceLibrary2
.IService1">- < identity>
- < dns value="localhost"/>
- < /identity>
- < /endpoint>
- < /service>
- < /services>
- < behaviors>
- < serviceBehaviors>
- < behavior name="WcfServiceLibrary2
.Service1Behavior">- < serviceDebug includeException
DetailInFaults="False" />- < serviceDebug includeException
DetailInFaults="False"/>- < /behavior>
- < /serviceBehaviors>
- < /behaviors>
以上就是針對WCF服務元數據的一些操作方法介紹。