【问题标题】:AllowAnonymous attribute being ignored in MVC4 with ClaimsAuthorizationManager使用 ClaimsAuthorizationManager 在 MVC4 中忽略 AllowAnonymous 属性
【发布时间】:2012-08-31 05:33:45
【问题描述】:

我正在尝试在一个新的 MVC4 应用程序中使用基于 Dominic Baier 出色工作的自定义 ClaimsAuthorizationManager,该应用程序仍然非常基于开箱即用的 Internet 模板,当我对其进行配置时,它似乎想要授权一切并忽略 [AllowAnonymous] 属性。

查看堆栈跟踪似乎是 ExtensionlessUrlHandler-Integrated-4.0 触发了 AuthorizationManager 代码。

我觉得这要么是我的 web.config 的配置问题(见下文),要么我的 AuthorizationManager 中需要一些额外的逻辑,以便当时不执行检查。

我这里的完整性是 Controller sn-p:

    [AllowAnonymous]
public class HomeController : Controller
{
    [AllowAnonymous]
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }

system.web 部分:

<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />
  <authorization>
    <!--<deny users="?" />-->
  </authorization>
  <authentication mode="Forms">
    <forms loginUrl="~/Account/Login" timeout="2880" />
  </authentication>
  <roleManager enabled="true" defaultProvider="simple">
    <providers>
      <clear />
      <add name="simple" type="WebMatrix.WebData.SimpleRoleProvider,WebMatrix.WebData" />
    </providers>
</roleManager>
<membership defaultProvider="simple">
    <providers>
      <clear />
      <add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider,WebMatrix.WebData" />
    </providers>
</membership>
<pages>
  <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>
<httpModules>
  <!--WIF 4.5 modules -->
  <!--Not needed here for IIS >= 7 -->
  <!--<add name="ClaimsAuthorizationModule" type="System.IdentityModel.Services.ClaimsAuthorizationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>-->
</httpModules>

system.webServer 部分:

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <handlers>
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
    <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
    <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
  <modules runAllManagedModulesForAllRequests="false">
    <remove name="RoleManager" />
    <remove name="FormsAuthentication" />

    <!--WIF 4.5 modules -->
    <add name="ClaimsAuthorizationModule" type="System.IdentityModel.Services.ClaimsAuthorizationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <add name="ClaimsTransformationModule" type="HillIntl.MyLCM.Security.ClaimsTransformationHttpModule" />

    <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
</modules>

身份模型部分:

<!--WIF 4.5 s.im section-->

【问题讨论】:

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


    【解决方案1】:

    当您添加声明 authZ 模块时,它确实会在每个请求上触发。您需要一个自定义的 [Authorize] 属性,以便 MVC4 与声明授权配合使用。

    与此类似:http://leastprivilege.com/2012/06/22/authorization-in-asp-net-web-api/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2018-05-19
      • 2016-04-11
      • 2013-05-15
      相关资源
      最近更新 更多