2008-11-13 40 views
7

我正在使用WCF服務和net.tcp端點,並將serviceAuthentication的主體PermissionMode設置爲UseWindowsGroups。在WCF中使用App.config中的Windows角色身份驗證

當前在服務的實現中,我使用PrincipalPermission屬性來爲每個方法設置角色需求。

 [PrincipalPermission(SecurityAction.Demand, Role = "Administrators")] 
     [OperationBehavior(Impersonation = ImpersonationOption.Required)] 
     public string method1() 

我試圖做幾乎相同的確切的事情,除了在app.config中設置角色的配置。有沒有辦法做到這一點,仍然使用Windows組身份驗證?

謝謝

回答

3

如果我理解的很好,您希望在運行時選擇角色。這可以通過WCF操作中的permission需求完成。例如。

public string method1() 
{ 
    PrincipalPermission p = new PrincipalPermission(null, "Administrators"); 
    p.Demand(); 
    ... 
8

如果你正在主持在IIS WCF服務,它會在ASP.NET工作進程中運行,這意味着你可以配置身份驗證和授權,你會與ASMX Web服務做:

<system.Web> 
    <authentication mode="Windows"/> 
    <authorization> 
     <allow roles=".\Administrators"/> 
     <deny users="*"/> 
    </authorization> 
</system.Web> 

然後,您將必須禁用在IIS中對您的端點的匿名訪問,而是啓用Windows集成身份驗證
在IIS管理控制檯中,通過爲虛擬目錄啓用'屬性'對話框來實現此目的。然後,您將在'目錄安全'選項卡中找到安全設置。

當然,唯一可用的通信通道是HTTP。客戶必須在傳輸級提供在請求他們的Windows標識使用這些設置:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="WindowsSecurity"> 
       <security mode="Transport"> 
        <transport clientCredentialType="Windows" /> 
       </security> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="https://localhost/myservice" 
        binding="wsHttpBinding" 
        bindingConfiguration="WindowsSecurity" 
        contract="IMyService" /> 
    </client> 
</system.serviceModel> 

請注意,如果你的服務端點使用的wsHttpBinding,那麼你也將有SSL添加到您的端點因爲這是WCF在使用傳輸級安全性時強制實施的要求。
如果你不是去爲basicHttpBinding的,你就能夠使用在WCF提供一個不太安全認證模式稱爲TransportCredentialOnly,這裏不再需要SSL。

有關更詳細的信息,here是WCF安全基礎結構的很好概述。

+3

請注意,如果你想這樣做,你需要在你的web.config文件中啓用ASP.NET兼容模式。否則它不會工作(也不會拋出錯誤)。 – Rocklan 2012-09-20 23:49:19

+0

注意:爲此,system.web/roleManager/@enabled必須爲false或省略,否則角色將是IIS .Net角色,而不是Windows組。 – Polyfun 2015-02-09 10:14:48