[英]WCF service without SSL but with Windows Group authentication
我們正在嘗試創建只能由指定的Windows組訪問的WCF服務。 如何在服務器web.config和客戶端配置中配置?
注意:我們希望能夠控制允許在服務器web.config中訪問的窗口組,而不是代碼。 此外,我們根本不需要/需要SSL。
我用Google搜索,然后我能找到的最好的例子都是這樣的......
但這並不能解釋如何限制只訪問特定組或組。
如果這是Intranet應用程序,您可以使用netTcpBinding:
<services>
<service name="YourService"
behaviorConfiguration="YourServiceBehavior">
<endpoint
binding="netTcpBinding"
bindingConfiguration="SecureTransportWindows"
contract="YourContract" />
</service>
</services>
<bindings>
<binding name="SecureTransportWindows">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="YourServiceBehavior">
<serviceAuthorization principalPermissionMode="UseWindowsGroups" />
</behavior>
</serviceBehaviors>
</behaviours>
然后在服務代碼中,您可以要求Windows角色:
class YourService : YourContract
{
[PrincipalPermission(SecurityAction.Demand, Role="MYDOMAIN\Administrators")]
public string SecuredOperation(string name)
{
return "secured operation";
}
}
如果需要在config中進行設置,則必須實現自定義授權:
<behavior name="YourServiceBehavior">
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="YourCustomAuthorizationPolicy"/>
</authorizationPolicies>
</serviceAuthorization>
</behavior>
並在代碼實現IAuthorizationPolicy接口:
public class YourCustomAuthorizationPolicy : IAuthorizationPolicy
{
//you need to check msdn
}
好的,這是我們提出的解決方案。 雖然它確實涉及代碼更改(添加AspNetCompatibilityRequirements屬性),但我們現在可以在web.config文件中實現組/角色的配置,而不是硬編碼。
這有很多步驟......
1)將aspNetCompatibilityEnabled屬性添加到serviceHostingEnvironment元素並設置為true,例如......
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
這告訴WCF服務在ASP.NET兼容模式下運行並完全參與ASP.NET HTTP請求生命周期。 有關完整詳細信息,請參閱此MSDN文章 。
2)在WCF代碼中,根據上面的鏈接和本MSDN文章中的規定,將AspNetCompatibilityRequirements屬性添加到服務類中......
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
3)現在我們可以添加通常的ASP 授權元素來限制對指定組/用戶的訪問(沒有上面的設置(1)和(2),這將被WCF忽略)...
<system.web>
<authorization>
<allow roles="MYDOMAIN\WCFAuthenticatedUsers" /> <-- allows access to users in this group
<deny users="*" /> <-- denies access to all other users
</authorization>
</system.web>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.