在带有WCF的App.config中使用Windows角色身份验证

use*_*444 7 wcf app-config windows-authentication

我正在使用WCF服务和net.tcp端点,其serviceAuthentication的主要PermissionMode设置为UseWindowsGroups.

目前在服务的实现中,我使用PrincipalPermission属性来设置每个方法的角色要求.

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

我尝试做的几乎完全相同,除了在app.config中设置角色的配置.有没有办法做到这一点,仍然使用Windows组身份验证?

谢谢

Enr*_*lio 8

如果您在IIS中托管WCF服务,它将在ASP.NET辅助进程中运行,这意味着您可以像对ASMX Web服务一样配置身份验证和授权:

<system.Web>
    <authentication mode="Windows"/>
    <authorization>
        <allow roles=".\Administrators"/>
        <deny users="*"/>
    </authorization>
</system.Web>
Run Code Online (Sandbox Code Playgroud)

然后,您必须禁用对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>
Run Code Online (Sandbox Code Playgroud)

请注意,如果您的服务端点使用wsHttpBinding,那么您还必须将SSL添加到端点,因为这是WCF在您使用传输级安全性时强制执行的要求.
如果您改为使用basicHttpBinding,则可以使用WCF中可用的安全性较低的身份验证模式,称为TransportCredentialOnly,不再需要SSL.

有关详细信息,这里是WCF安全基础结构的一个很好的概述.

  • 请注意,如果要执行此操作,则需要在web.config文件中启用ASP.NET兼容模式.否则它将无法工作(也不会抛出错误). (3认同)

Pan*_*nos 3

如果我理解得很好,您想在运行时选择角色。这可以通过 WCF 操作中的权限需求来完成。例如

public string method1()
{
    PrincipalPermission p = new PrincipalPermission(null, "Administrators");
    p.Demand();
    ...
Run Code Online (Sandbox Code Playgroud)