无法在 web.config 中为 WCF 服务设置授权规则

发布于 2024-10-17 00:23:50 字数 2837 浏览 3 评论 0原文

我在一篇文章中读到,您可以在 Web 配置中使用 ASP.Net 授权来控制对 WCF Web 服务的访问,以替换以下属性:

[PrincipalPermission(SecurityAction.Demand, Role="Administrators")]

为了测试,我一直在使用“管理员”,这是一个有效的角色,因此应该允许我访问和“测试”,但不是。使用上述属性时效果很好,但是当我注释掉该属性并在我的 Web.Config 文件中使用它时:

<authentication mode="Windows" />
<authorization>
  <allow roles=".\TEST"/>
  <deny roles="*"/>
</authorization>

它仍然允许我访问。

所以我想知道我的 web.config 是否有问题,或者我读到的使用它的说法是否错误。

仅供参考,这是我查看的帖子:

使用App.config 中使用 WCF 进行 Windows 角色身份验证

,以下是我的 web.config:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Windows" />
    <authorization>
      <allow roles=".\TEST"/>
      <deny users="*"/>
    </authorization>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpEndpointBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="WcfService1.ServiceBehaviour1" name="WcfService1.Service1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding"
                  name="BasicHttpEndpoint" contract="WcfService1.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.ServiceBehaviour1">
          <!-- 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>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

谢谢。

I read on a post that you can use ASP.Net authorization in the web config to control access to a WCF web service to replace the following attribute:

[PrincipalPermission(SecurityAction.Demand, Role="Administrators")]

To test I have been using "Administrators" which is a valid role so should allow me access and "TEST" which isnt. This works fine when using the above attribute however when I comment that out and use this in my Web.Config file:

<authentication mode="Windows" />
<authorization>
  <allow roles=".\TEST"/>
  <deny roles="*"/>
</authorization>

It still allows me access.

So I was wondering if I have just got something wrong in the web.config or whether what I read was wrong saying to use that.

Just for reference this is the post I looked at:

Using Windows Role authentication in the App.config with WCF

and the following is my web.config:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Windows" />
    <authorization>
      <allow roles=".\TEST"/>
      <deny users="*"/>
    </authorization>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpEndpointBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="WcfService1.ServiceBehaviour1" name="WcfService1.Service1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding"
                  name="BasicHttpEndpoint" contract="WcfService1.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.ServiceBehaviour1">
          <!-- 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>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2024-10-24 00:23:50

如果 WCF 并行托管,则 ASP.NET 授权规则不适用于 WCF 服务与 ASP.NET。要使用它们,您必须将 WCF 服务设置为 ASP.NET 兼容模式

ASP.NET authorization rules are not used for WCF services if WCF is hosted side-by-side with ASP.NET. To use them you must turn your WCF service to ASP.NET compatibility mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文