[英]How to configure WCF service deployed on IIS and remote client to authenticate from remote client PC?
我是個菜鳥; 請幫助我理解這個認證配置/綁定讓我困惑的東西。
我在Win 2008上的IIS 7上部署了C#WCF服務。我的客戶端是Windows Forms C#應用程序。 當我的客戶端從運行WCF服務的同一台服務器運行時,我的客戶端運行正常,但是當我嘗試從遠程PC運行我的客戶端時,我得到以下異常......
System.ServiceModel.Security.SecurityNegotiationException:服務未對調用方進行身份驗證。
我已經閱讀了一些關於這些問題的帖子,並且知道我的問題是因為我的服務和客戶端配置為使用Windows身份驗證,我猜這是使用Visual Studio創建服務時的默認設置,以及添加服務引用給客戶。 在我進行任何更改之前,下面是我的配置,當它仍然設置為Windows時(刪除了不相關的位)...
Web.Config中
<system.web>
...
<authentication mode="Windows"/>
...
<system.serviceModel>
<services>
<service name="MCLaborServer.LaborService" behaviorConfiguration="MCLaborServer.LaborServiceBehavior">
<!-- Service Endpoints -->
<endpoint address="" binding="wsHttpBinding" contract="MCLaborServer.ILaborService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MCLaborServer.LaborServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
從客戶端的App.Config ...
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ILaborService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://<myDnsNameGoesHere>/MCLaborServer/LaborService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ILaborService"
contract="LaborService.ILaborService" name="WSHttpBinding_ILaborService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
所以,首先我在web.config中更改了“authentication mode =”None“”,並在客戶端的app.config中設置了“security mode =”None“”,並為消息和傳輸設置了clientCredentialType =“None”。 我還在web.config和客戶端的app.config中注釋掉了“identity”部分。 雖然完全打破了它,現在在本地運行的客戶端甚至無法工作; 它給出了“遠程服務器返回意外響應:(405)方法不允許”錯誤。
那么我可以做些什么來關閉安全性以便我可以使用遠程客戶端進行連接? 我確實通過IIS為我的應用程序啟用了匿名訪問。
我還想問一下,配置這個的最佳實踐方法是什么,這樣我就可以通過互聯網以半安全的方式在遠程客戶端上進行web服務調用,而無需使用SSL或做任何花錢的事情。 我真的不關心數據的安全性,因為它不是真正敏感的數據,但我仍然想確保服務器不會受到攻擊。
另外,我讀到我可以使用Windows身份驗證,然后在代碼中明確指定憑據,如下所示。 如果我這樣做,它還可以遠程工作嗎? 如果是這樣,最終是否會使我的服務器的Windows憑據以不安全的方式通過網絡發送,那么我是否願意讓我的憑據被劫持?
SomeService.ServiceClient someService = new SomeService.ServiceClient();
someService.ClientCredentials.Windows.ClientCredential.UserName="windowsuseraccountname"
someService.ClientCredentials.Windows.ClientCredential.Password="windowsuseraccountpassword"
我已閱讀以下帖子/鏈接,但仍感到困惑。 謝謝你的幫助!
http://msdn.microsoft.com/en-us/library/aa291347(v=vs.71).aspx
在設置跨域運行的低安全性WCF服務時,我們遇到了類似的問題。 最大的問題之一(如果可以稱之為)是WCF默認配置為非常安全。 因為我們的應用程序完全在一個安全的網絡中,所以我們不想打擾許多復雜的證書。 我們的解決方法是創建一個自定義綁定,允許我們對我們的服務使用用戶名/密碼身份驗證,而無需任何加密。 我們的實現基於Yaron Naveh的 Clear Username Binding 。 我建議你看一下(並在他的博客文章中介紹它 )。
有關WCF綁定和安全性的一些很好的資源:
我通過更改綁定到basicHttpBinding,將身份驗證更改為Forms並關閉安全性來修復此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.