4

我的 ASP.NET MVC4 Web 应用程序有一个自定义主体/身份。我还创建了一个 AuthorizeAttribute 来实例化我的自定义主体,将其分配给我需要身份验证的控制器中的 httpContext.User。

这对于用我的 AuthorizeAttribute 装饰的控制器/操作非常有用,但是,对于不需要身份验证的控制器(但如果它存在,仍然使用它),我想获得我的 CustomPrincipal (最好通过 HttpContext.User ) .

在这些未修饰的控制器/操作中,设置了 HttpContext.User,但使用的是 GenericPrincipal 而不是我的 CustomPrincipal。将 HttpContext.User 的默认设置“覆盖”到 GenericPrincipal 的最佳位置在哪里?

同样,如果在每个具有身份验证 cookie 的请求中都执行此操作,那么我将如何避免在 AuthorizeAttribute 装饰控制器的情况下执行两次工作(然后它将成为强制身份验证的控制器)。

为了清楚起见,我的网站允许匿名用户访问,但是在这些页面上,如果一个经过身份验证(并且实现了 CustomPrincipal),则提供了额外的功能。

我认为一些选项是(不确定每个选项背后的逻辑):

  • 使用会话(并处理逻辑来创建我需要的东西,忘记校长)
  • Application_AuthenticateRequest - 在网上看到评论说这是老派
  • 在基本控制器上设置的自定义过滤器
  • 在基本控制器上创建一个 AuthorizationAttribute,让每个人都可以通过并根据需要设置 HttpContext.User
  • IHttpModule - 这似乎是一种下降方式(除非其他人不同意,否则沿着这条路前进)。

想法?

4

2 回答 2

19

您可以使用全局操作过滤器。假设您有一个自定义主体:

public class MyPrincipal : GenericPrincipal
{
    public MyPrincipal(IIdentity identity, string[] roles): base(identity, roles)
    {
    }

    ... some custom properties and stuff
}

然后您可以编写一个全局授权操作过滤器(但它不是从基础派生AuthorizeAttribute以避免全局身份验证,它只是实现IAuthorizationFilter接口以确保它在任何其他过滤器之前运行):

public class GlobalIdentityInjector : ActionFilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var identity = filterContext.HttpContext.User.Identity;

        // do some stuff here and assign a custom principal:
        var principal = new MyPrincipal(identity, null);
        // here you can assign some custom property that every user 
        // (even the non-authenticated have)

        // set the custom principal
        filterContext.HttpContext.User = principal;
    }
}

全局过滤器将被注册,~/App_Start/FilterConfig.cs以确保它适用于所有操作:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new GlobalIdentityInjector());
    }
}

现在您可以拥有一个自定义授权属性,该属性仅适用于某些需要身份验证的控制器操作:

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var authorized = base.AuthorizeCore(httpContext);
        if (!authorized)
        {
            return false;
        }

        // we know that at this stage we have our custom
        // principal injected by the global action filter
        var myPrincipal = (MyPrincipal)httpContext.User;

        // do some additional work here to enrich this custom principal
        // by setting some other properties that apply only to
        // authenticated users

        return true;

    }
}

然后你可以有两种类型的动作:

public ActionResult Foo()
{
    var user = (MyPrincipal)User;

    // work with the custom properties that apply only
    // to anonymous users

    ...
}

[MyAuthorize]
public ActionResult Bar()
{
    var user = (MyPrincipal)User;

    // here you can work with all the properties
    // because we know that the custom authorization
    // attribute set them and the global filter set the other properties

    ...
}
于 2012-07-14T10:21:47.700 回答
0

覆盖主体:

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)

代替

protected void Application_AuthenticateRequest(object sender, EventArgs e)

在 Global.asax.cs 中为我工作的 ASP Web 应用程序

于 2013-10-22T14:46:03.297 回答