探討WCF安全中的服務(wù)元數(shù)據(jù)保護
實際,我最關(guān)心的是WCF的安全問題,網(wǎng)上不少朋友介紹的WCF安全也是少得可憐,微軟發(fā)布的WCFSecurityGUID好像講得也只是入門級別的教程,離真正應(yīng)用到項目中還是有很大的距離,這也讓我萌發(fā)了分享的想法,今天先放出來占個位置吧,有反對的朋友磚頭輕點,呵~,可以告訴你,WCF的安全里,有很多的小秘密,當然還是要告訴你,并且有此小秘密是要自己去體驗后才知道,在博客排版方面,李會軍(軍哥)讓人感覺最舒服,在解說方面,軍哥也是以簡潔著稱,我在這里也學(xué)習(xí)一下,一起簡潔吧,我希望以后的WCF安全探討里,一次只講一個小內(nèi)容好了~
WCF安全概述
WindowsCommunicationFoundation(WCF)是Microsoft為構(gòu)建面向服務(wù)的應(yīng)用程序而提供的統(tǒng)一編程模型(摘自MSDN),在分布式環(huán)境下的WCF安全問題尤為重要,如果你覺得使用了WCF默認的安全措施可以讓你高枕無憂,那明天你可就以回家種田了,當然,對于學(xué)習(xí)來說,足夠了~,但我們講的是真正的項目應(yīng)用,WCF在各種協(xié)議下的安全提供和保證是不盡相同的。
背景
某天,經(jīng)理老陳對程序員小李說:小李,我們公司外包到一個項目,但是客戶要求采用分布式部署,現(xiàn)在項目快接近尾聲了,由于我們采用的是WCF,所以在部署的時候出現(xiàn)了一點問題,我們的服務(wù)好像誰都能訪問得到啊,這是為什么呢?
WCF安全問題呈現(xiàn)
小李***件事就是去查看了服務(wù)配置文件,真的是不看不知道,一看嚇一跳,原來開發(fā)WCF時,采用的都是默認的配置,全是自動生成的代碼,沒有經(jīng)過任何的改動,一想到項目將會以這種姿態(tài)交付,小李著實捏了一把汗。
- Code
- <services>
- <servicenameservicename="WcfServiceLibrary2.Service1"behaviorConfiguration="WcfServiceLibrary2.Service1Behavior">
- <host>
- <baseAddresses>
- <addbaseAddressaddbaseAddress="http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/"/>
- </baseAddresses>
- </host>
- <endpointaddressendpointaddress=""binding="wsHttpBinding"contract="WcfServiceLibrary2.IService1">
- <identity>
- <dnsvaluednsvalue="localhost"/>
- </identity>
- </endpoint>
- <endpointaddressendpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behaviornamebehaviorname="WcfServiceLibrary2.Service1Behavior">
- <serviceMetadatahttpGetEnabledserviceMetadatahttpGetEnabled="True"/>
- <serviceDebugincludeExceptionDetailInFaultsserviceDebugincludeExceptionDetailInFaults="False"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
解決之道
小李***件事就是把配置文件給修改好了,接著解決了困擾老陳許久的問題。
1、刪除元數(shù)據(jù)交換終結(jié)點信息
- <endpointaddressendpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
2、將http協(xié)議獲取元數(shù)據(jù)重置為:false
- <serviceMetadatahttpGetEnabledserviceMetadatahttpGetEnabled="false"/>
3、一般我們都會在開發(fā)時配置為元數(shù)據(jù)據(jù)可發(fā)現(xiàn),但是切記,發(fā)布你的服務(wù)前,一定要刪除了,目前,服務(wù)在一定范圍上得到了保護
4、最終配置如下
- <services>
- <servicenameservicename="WcfServiceLibrary2.Service1"behaviorConfiguration="WcfServiceLibrary2.Service1Behavior">
- <host>
- <baseAddresses>
- <addbaseAddressaddbaseAddress="http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary2/Service1/"/>
- </baseAddresses>
- </host>
- <endpointaddressendpointaddress=""binding="wsHttpBinding"contract="WcfServiceLibrary2.IService1">
- <identity>
- <dnsvaluednsvalue="localhost"/>
- </identity>
- </endpoint>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behaviornamebehaviorname="WcfServiceLibrary2.Service1Behavior">
- <serviceDebugincludeExceptionDetailInFaultsserviceDebugincludeExceptionDetailInFaults="False"/>
- <serviceDebugincludeExceptionDetailInFaultsserviceDebugincludeExceptionDetailInFaults="False"/></behavior></serviceBehaviors></behaviors>
【編輯推薦】