未调用自定义AuthorizeAttribute

时间:2013-06-18 01:31:34

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

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

如果我使用[授权]我会收到提示输入用户名和密码但如果我使用[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);
         }

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

   <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" />

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

新信息: 我的属性位于共享DLL中,因为它被许多应用程序使用。似乎如果我将cs文件复制到web项目它是有效的。不知道为什么,仍在寻找提示或提示。

1 个答案:

答案 0 :(得分:3)

根据您的说法,如果您使用[Authorize]但不使用[InternalAuthorizeV2],则一切正常。

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

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

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

测试依赖关系

您可以尝试将基本授权属性添加到共享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
  }
}

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

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

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

授权链

请注意,您已将属性附加到Action方法,因此只有在链中较早的授权属性(例如全局过滤器或控制器属性)尚未阻止用户获得授权时才会触发该属性(请参阅my answer here),或者过早地返回一个ActionResult,它会阻止链到达Action属性。但是,如果只是将它从dll移动到项目中就不会出现问题。同样地,你不可能在你所说的错误的地方AllowAnonymous

相关问题