【问题标题】:Why won't Authorization Rules in IIS restrict access to my WCF service?为什么 IIS 中的授权规则不限制对我的 WCF 服务的访问?
【发布时间】:2021-08-15 03:24:48
【问题描述】:

我在 IIS 10 中托管了一个独立的 WCF 服务。我想将对该 Web 服务的访问限制为一组选定的用户。通过在 IIS 中执行以下操作,我能够为 Web 应用程序执行此操作:

  • 身份验证:仅 Windows 身份验证(禁用匿名身份验证)
  • 授权规则:允许预定义的组(即角色)

但是,当我为 Web 服务执行上述步骤并在其 web.config 中更改 clientCredentialType="Windows" 时,它仍然允许域中的任何用户与其交谈。我错过了一些明显的东西吗?在配置授权方面,Web 服务的功能是否与 Web 应用程序不同?鉴于我的设置,我希望只有 MyTestGroup 中的用户能够与 Web 服务对话,而所有其他用户都会得到 401 - Unauthorized。

顺便说一句,我尝试设置“拒绝所有人”规则,但域用户仍然可以与 Web 服务对话,所以我觉得授权设置没有以某种方式生效。寻找对此的任何见解。

以下是相关的 web.config 内容:

  <system.serviceModel>
    <services>
      <service name="StudyManagement.StudyManagement">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="secureHttpBinding" name="StudyManagement" contract="StudyManagement.IStudyManagement" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <bindings>
      <basicHttpBinding>
        <binding maxReceivedMessageSize="1048576" />
        <binding name="secureHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" minFreeMemoryPercentageToActivateService="0" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="false" />
        <security>
            <authorization>
                <remove users="*" roles="" verbs="" />
                <add accessType="Allow" users="" roles="MyAllowGroup" />
            </authorization>
        </security>
  </system.webServer>

【问题讨论】:

  • 额外的侦查发现了另一个 Stack Overflow 问题,该问题似乎与我的相同,或者至少在性质上非常相似。几年前问过,没有任何有用的回答。不会让我对寻求洞察力抱有希望! stackoverflow.com/questions/39829002/…

标签: wcf authorization web-config windows-authentication iis-10


【解决方案1】:

您可以参考此website 重新创建授权规则。另外wcf服务的授权可以使用ServiceAuthenticationmanager,examples and tutorials.

【讨论】:

  • 感谢您的回复,但是,我的问题不在于为网页设置授权。那部分很好。我的问题专门针对 WCF 服务 (.svc)。我也真的不想沿着创建自己的身份验证验证器的路线走下去。希望只使用内置的 IIS Windows 身份验证/授权规则。
【解决方案2】:

以下 Microsoft 文档帮助回答了我的问题:

WCF services and ASP.NET

重要提示:

ASP.NET HTTP 运行时处理 ASP.NET 请求,但不参与处理发往 WCF 服务的请求,即使这些服务与 ASP 托管在同一个 AppDomain 中。网络内容。相反,WCF 服务模型截获发送给 WCF 服务的消息,并通过 WCF 传输/通道堆栈路由它们。

在 AppDomain 中,由 HTTP 运行时实现的功能适用于 ASP.NET 内容,但不适用于 WCF。 ASP.NET 应用程序平台的许多 HTTP 特定功能不适用于托管在包含 ASP.NET 内容的 AppDomain 内的 WCF 服务。这些功能的示例包括:

  • 基于文件的授权: WCF 安全模型在决定是否授权服务请求时不允许将访问控制列表 (ACL) 应用于服务的 .svc 文件。 p>

  • 基于配置的 URL 授权:类似地,WCF 安全模型不遵守在 System.Web 的配置元素中指定的任何基于 URL 的授权规则。 如果服务驻留在受 ASP.NET 的 URL 授权规则保护的 URL 空间中,则 WCF 请求将忽略这些设置。

解决办法:

通过配置 web.config 使用 ASP.NET 兼容模式

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

与默认的并行配置不同,WCF 托管基础结构拦截 WCF 消息并将它们路由到 HTTP 管道之外,在 ASP.NET 兼容模式下运行的 WCF 服务完全参与 ASP.NET HTTP 请求生命周期。在兼容模式下,WCF 服务通过 IHttpHandler 实现使用 HTTP 管道,类似于处理 ASPX 页面和 ASMX Web 服务请求的方式。因此,在以下 ASP.NET 功能方面,WCF 的行为与 ASMX 相同:

  • 基于文件的授权:在 ASP.NET 兼容模式下运行的 WCF 服务可以通过将文件系统访问控制列表 (ACL) 附加到服务的 .svc 文件来确保安全。
  • 可配置的 URL 授权:当 WCF 服务在 ASP.NET 兼容模式下运行时,对 WCF 请求强制执行 ASP.NET 的 URL 授权规则。

我建议阅读整篇文章以获取更多信息。这是一篇简短而有用的读物​​。


感谢@Shiraz Bhaiji 在WCF Authorization using IIS and ACLs 上的文章参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2011-08-29
    • 2012-07-23
    相关资源
    最近更新 更多