如何保護(hù)WCF服務(wù)元數(shù)據(jù)
WCF服務(wù)元數(shù)據(jù)的保護(hù)方法是一個(gè)比較復(fù)雜的步驟。相信對于初學(xué)者來說,很難操作這一方法。在這里我們就為大家介紹一下相關(guān)方法技巧。#t#
概述
WCF是 Microsoft 為構(gòu)建面向服務(wù)的應(yīng)用程序而提供的統(tǒng)一編程模型(摘自MSDN),在分布式環(huán)境下的安全問題尤為重要,如果你覺得使用了WCF默認(rèn)的安全措施可以讓你高枕無憂,那明天你可就以回家種田了,當(dāng)然,對于學(xué)習(xí)來說,足夠了~,但我們講的是真正的項(xiàng)目應(yīng)用,WCF在各種協(xié)議下的安全提供和保證是不盡相同的。
背景
某天,經(jīng)理老陳對程序員小李說:小李,我們公司外包到一個(gè)項(xiàng)目,但是客戶要求采用分布式部署,現(xiàn)在項(xiàng)目快接近尾聲了,由于我們采用的是WCF,所以在部署的時(shí)候出現(xiàn)了一點(diǎn)問題,我們的服務(wù)好像誰都能訪問得到啊,這是為什么呢?
WCF服務(wù)元數(shù)據(jù)問題呈現(xiàn)
小李***件事就是去查看了服務(wù)配置文件,真的是不看不知道,一看嚇一跳,原來開發(fā)WCF時(shí),采用的都是默認(rèn)的配置,全是自動(dòng)生成的代碼,沒有經(jīng)過任何的改動(dòng),一想到項(xiàng)目將會以這種姿態(tài)交付,小李著實(shí)捏了一把汗。
- < 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服務(wù)元數(shù)據(jù)解決之道
小李***件事就是把配置文件給修改好了,接著解決了困擾老陳許久的問題。
1、刪除元數(shù)據(jù)交換終結(jié)點(diǎn)信息
- < endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
2、將http協(xié)議獲取元數(shù)據(jù)重置為:false
- < serviceMetadata
httpGetEnabled="false"/>
3、一般我們都會在開發(fā)時(shí)配置為WCF服務(wù)元數(shù)據(jù)可發(fā)現(xiàn),但是切記,發(fā)布你的服務(wù)前,一定要?jiǎng)h除了,目前,服務(wù)在一定范圍上得到了保護(hù)
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服務(wù)元數(shù)據(jù)的一些操作方法介紹。