【问题标题】:Exclude specific path from WIF authorization in a ASP.NET MVC 4 project从 ASP.NET MVC 4 项目中的 WIF 授权中排除特定路径
【发布时间】:2012-10-28 12:28:32
【问题描述】:

借助 Visual Studio 2012 的 Identity and Access... 扩展,我们已在 ASP.NET 4.5 MVC 4 项目中成功配置 Windows Identity Foundation (WIF)。但无法从授权中排除特定路径以允许匿名访问。

当我们访问我们的默认路由(即/Home)时,被动重定向会将我们重定向到配置的颁发者 Uri。这是正确的。但现在假设我们要从 STS 身份验证中排除路径 /Guest,以便每个人都可以访问 http://ourhost/Guest 而无需路由到 STS 颁发者。那里只有静态文档。

来自Web.config的片段:

<system.identityModel>
  <identityConfiguration>
    <audienceUris>
      <add value="http://ourhost/" />
    </audienceUris>
    <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <trustedIssuers>
        <add thumbprint="9B74****40D0" name="OurSTS" />
      </trustedIssuers>
    </issuerNameRegistry>
    <certificateValidation certificateValidationMode="None" />
  </identityConfiguration>
</system.identityModel>
<system.identityModel.services>
  <federationConfiguration>
    <cookieHandler requireSsl="false" />
    <wsFederation passiveRedirectEnabled="true" issuer="http://oursts/Issue" realm="http://ourhost/" reply="http://ourhost/" requireHttps="false" />
  </federationConfiguration>
</system.identityModel.services>

我们还有...

<system.webServer>
  <!-- ... -->
  <modules runAllManagedModulesForAllRequests="true">
    <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
    <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
    <remove name="FormsAuthentication" />
  </modules>
</system.webServer>

最后:

<system.web>
  <!-- ... -->
  <authentication mode="None" />
</system.web>

我们尝试了以下方法但没有成功:

<location path="~/Guest"> <!-- also "/Guest" is not working -->
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>

我们还尝试将一个小的 Web.config 文件放入此文件夹,但没有成功。无论我们在浏览器中定位到哪个 Uri,我们总是会被重定向。

实现此目的的正确方法是什么?

编辑

删除了之前的“已接受回答”,将“已接受回答”设置为Eugenios answer,因为这是更有用的回复。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 wif claims-based-identity


    【解决方案1】:

    在 MVC 应用程序中,您通常通过控制器和操作中的 [Authorize] 属性定义访问权限。

    只需从 web.config 中删除:

    <system.web>
         <authorization>
            <deny users="?" />
          </authorization>
    

    注意:这通常由 VS2010 中的“添加 STS 引用” 向导自动添加

    似乎在 VS2012 和新工具上的行为完全相同。我刚刚创建了一个全新的 MVC4 应用程序。使用本地配置 STS 运行“身份和访问...”工具(保留所有默认值)。

    它确实将此片段添加到 web.config:

    <authorization>
      <deny users="?" />
    </authorization>
    

    我将其删除并将[Authorize] 添加到 About 控制器操作中:

    [Authorize]
    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";
    
        return View();
    }
    

    当我点击“关于”链接时,我会被重定向到 STS。其他一切都适用于匿名访问。

    注意:

    您也可以在向导中对此进行一些控制(请参阅向导的“配置”页面)。

    【讨论】:

    • 感谢 Eugenio,我们正在使用 Visual Studio 2012 并且“添加 STS 参考”不再可用,您必须改用“身份和访问...”工具。我想我已经接近解决方案了,看起来passiveRedirectEnabled="true" 正在将我重定向到 STS,而不管使用任何属性。设置为false 并在自定义身份验证属性中使用一些重定向助手进行重定向现在看起来相当不错。完成后会发布我的解决方案,谢谢!
    • 我明白了。它也应该与 passiveRedirectEnabled=true 一起使用。我也会检查我的系统。
    • 因为我在启用passiveRedirectEnabled选项时确实遇到了问题,所以我想接受我自己的答案作为解决方案,但我非常感谢你在这个主题上的帮助,我相信它可能也有效,但是也许解决方案隐藏在我的 web.config 设置与 IIS 设置相结合的某个地方。感谢您的宝贵时间,非常感谢!
    • 有没有办法在 WebForms 中执行 [Authorize] 方法?我不认为它是,但我没有找到任何替代方案。我希望在 web.config 中有一个解决方案。
    【解决方案2】:

    最终为我指明了正确方向的是旧版 blog post,它解释了如何保护特定控制器或页面区域。结合全局过滤器,我快到了。

    似乎关键是不要使用passiveRedirectEnabled="true" 选项,而是将其设置为false。只有这样您才能完全控制身份验证过程,但需要自己触发被动重定向,使用 SignInRequestMessage 类(这没什么大不了的)。

    欢迎使用更少代码的更好解决方案。

    编辑

    为此移除了“已接受回答”状态,将“已接受回答”设置为 Eugenios anwer,因为这是更有用的回复。

    【讨论】:

      【解决方案3】:

      我和托马斯的情况一样。就我而言,我在本地测试/使用 IISExpress。

      Eugenio 的回答几乎让我开始工作,但增加了一项要求。我必须将 MVC 项目属性中的“匿名身份验证”设置为“启用”。

      这要么在默认情况下被禁用,要么在使用 VS 2012“身份和访问...”工具时可能设置为这种方式。

      因此,回顾一下,没有要编写/维护的代码或特殊属性。

      我的 csproj 文件包含:

      <IISExpressAnonymousAuthentication>enabled</IISExpressAnonymousAuthentication>
      

      我的 web.config 包含:

      <system.web>
          <authentication mode="None" />
      </system.web>
      
      <system.web>
          <authorization>
              <allow users="*" />
          </authorization>
      </system.web>
      
      <system.webServer>
          <modules>
              <remove name="FormsAuthentication" />
              <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
              <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
          </modules>
      </system.webServer>
      
      <system.identityModel.services>
          <federationConfiguration>
              <wsFederation passiveRedirectEnabled="true" issuer="https://REMOVED.accesscontrol.windows.net/v2/wsfederation" realm="urn:REMOVED" requireHttps="false" />
          </federationConfiguration>
      </system.identityModel.services>
      

      并且,我将标准 [Authorize] 属性添加到我希望由 WIF 保护的控制器操作:

      [Authorize]
      public ActionResult About()
      {
      ....
      }
      

      【讨论】:

        【解决方案4】:

        我无法让[Authorize] 工作 - 它没有重定向到我的 STS,我确信这是我缺少的东西。不过,我确实发现了如何解决最初的问题。

        global.asax

            protected void Application_Start()
            {
                ... config stuff ...
                FederatedAuthentication.WSFederationAuthenticationModule.AuthorizationFailed += WSFederationAuthenticationModule_AuthorizationFailed;
            }
        

        然后:

            void WSFederationAuthenticationModule_AuthorizationFailed(object sender, AuthorizationFailedEventArgs e)
            {
                // Do path/file detection here
                if (Request.Path.Contains("/Content/") || Request.Path.Contains("/Scripts/"))
                {
                    e.RedirectToIdentityProvider = false;
                }
            }
        

        【讨论】:

        • 谢谢,Andrew,不过我想指出的是,这种方法会造成安全漏洞,因为我现在可以访问我想要的任何控制器和操作,因为我可以在末尾添加类似这样的内容url:“#/Content/”,这不会改变初始路由,但它会让我访问控制器。
        • 这对我不起作用,而接受的答案无需任何额外更改即可工作。我仍然收到 401 响应。
        【解决方案5】:

        我在 web.config 中解决了这个问题,第一行允许所有未经授权的用户,第二行禁用重定向

         <wsFederation passiveRedirectEnabled="false" issuer="xxx" realm="xxx"/>
        
        <authentication mode="Windows" />
          <authorization>
              <allow users="*" />
          </authorization>
        

        【讨论】:

        • 现有答案似乎很好地涵盖了这一点。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-27
        • 1970-01-01
        • 1970-01-01
        • 2014-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多