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