【问题标题】:Custom AuthorizeAttribute not called未调用自定义 AuthorizeAttribute
【发布时间】:2013-06-20 00:25:41
【问题描述】:

有很多类似的问题,但这让我很难过。

如果我使用 [Authorize],我会提示输入用户名和密码,但如果我使用 [InternalAuthorizeV2],我不会

我有一个自定义的 AuthorizeAttribute,目前它没有做任何特别的事情(我限制了可能出错的事情)。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class InternalAuthorizeV2Attribute: AuthorizeAttribute
    {}

和我的控制器中的一个动作

   [InternalAuthorizeV2(Roles = "MobileApps_Parkingggg")]
         public ActionResult Index()
         {
             var model = new VmParking();
             return View(model);
         }

登录在不同的应用程序中处理,但它们具有相同的网络配置行

   <machineKey compatibilityMode="Framework20SP2" validationKey="editedOut" decryptionKey="editedOut" validation="SHA1" decryption="AES"/>
      <authentication mode="Forms">
          <forms name="emLogin" loginUrl="/Login/Index" timeout="540" protection="All" path="/"  enableCrossAppRedirects="true"  cookieless="UseCookies" />
      </authentication>
    <sessionState timeout="540" />

我知道,如果我通过使用 [Authorize] 进入页面登录,然后返回我的问题页面,我可以看到用户名,但它似乎没有调用我的客户属性。

新信息: 我的属性位于共享 DLL 中,因为它被许多应用程序使用。看来,如果我将 cs 文件复制到 web 项目,它就可以工作。不知道为什么,仍在寻找提示或技巧。

【问题讨论】:

  • 请确认您的自定义属性子类 System.Web.Mvc.AuthorizeAttribute 而不是 System.Web.Http.AuthorizeAttribute。
  • 是的,我看到了,并三次检查它是。

标签: c# authentication asp.net-mvc-4 authorize-attribute


【解决方案1】:

根据您所说,如果您使用 [Authorize] 而不是 [InternalAuthorizeV2],则一切正常。

如果设置正确,您的共享 dll 应该不会有任何影响;我有同样的工作。确保 Web 项目使用的是最新版本的 dll,并且您在共享 dll 中拥有正确的程序集引用 - 在我的项目中为 System.Web.Mvc, v4.0.0.0

你说它被许多应用程序使用?共享 dll 是否所有应用程序都存在相同问题,或者只有其中一个问题?如果只有一个,请检查有问题的那个的参考。

如果以下测试全部正常,那么最后一个选项是,无论您在 dll 中的授权属性中做什么,都没有为该应用程序选择正确的上下文,或者使用正确的成员资格提供程序或数据库 - 你没有t 包含您在属性中使用的代码,因此很难知道这是否会导致问题。

测试依赖项

您可以尝试将基本授权属性添加到您的共享 dll,然后在您的 Web 项目中实现另一个授权属性,该属性继承您刚刚创建的基本属性。这应该表明您的共享 dll 设置正确。

// in the dll:
public class BaseAuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute { ... }

// in the web project:
public class InternalAuthorizeV2Attribute : BaseAuthorizeAttribute { ... }

如果只是将它从您的 dll 项目移动到 web 项目来修复它,最可能的问题是 web 项目没有使用正确的 dll 版本(尝试清理并进行完全重建)或者您的 dll 引用了System.Web.Mvc.AuthorizeAttribute 的 dll 错误。你说你已经三重检查了,但是尝试上面的调试应该可以帮助你确定是否真的是这种情况。

调试授权方法调用

如果这不起作用,请尝试将以下覆盖方法添加到一个非常简单的属性中,并查看您是否在调用 base.OnAuthorization 时遇到断点。如果你不这样做,那么它可能不是导致你的问题的实际属性。

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, 
  Inherited = true, AllowMultiple = true)]
public class InternalAuthorizeV2Attribute : System.Web.Mvc.AuthorizeAttribute {
  protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) {
    return false; // breakpoint here, and this should force an authorization failure
  }
  public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
  {
    base.OnAuthorization(filterContext); // breakpoint here
  }
}

这应该会完全阻止任何用户访问该操作。如果这不起作用,那么您知道问题不在于您的属性,而是您的属性没有被应用。

您还可以将以下内容添加到您的控制器并检查它是否在授权属性之前被命中:

protected override void OnAuthorization(AuthorizationContext filterContext) {
    base.OnAuthorization(filterContext);
}

授权链

请注意,您已将属性附加到 Action 方法,因此只有在链中较早的授权属性(例如全局过滤器或控制器属性)尚未阻止用户被授权时才会命中该属性(请参阅my answer here),或者过早地返回一个 ActionResult 来阻止链到达你的 Action 属性。但是,如果简单地将其从 dll 移至项目使其工作,则这不太可能是问题。同样,从您所说的情况来看,您不太可能在错误的位置使用 AllowAnonymous

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2020-01-13
    • 2012-09-22
    相关资源
    最近更新 更多