MVC 4 - 在beginrequest和endrequest之间丢失自定义主体

时间:2013-02-26 16:10:42

标签: c# asp.net-mvc asp.net-mvc-4 httpcontext

在我的项目(c#)中,我们有一个HttpModule,它创建了一个我们附加到CurrentPrincipal的自定义Principal。

这适用于我们所有的MVC3应用程序和我们的经典ASP.Net应用程序。

我们使用AuthorizeAttribute覆盖来保护我们的Controller方法 - 看起来非常标准。

问题是在自定义authorizeAttribute中,用户(httpContext.User)是RolePrincipal而不是自定义主体。

要进行故障排除,我在global.asax中放了一些处理程序来捕获beginrequest()和endrequest()。好吧,在BeginRequest中,我的用户就是我们所期望的 - 自定义主体。在EndRequest中,用户再次成为RolePrincipal。 HttpModule的web.config声明很好 - 我可以通过HttpModule的代码。

有谁知道发生了什么事?我们有一个自定义的HttpModule,但是我不能修改这个项目的代码(它在任何地方都可以使用,而且工作正常)。

这是我的第一个MVC4项目 - 我想知道MVC4是否做了不同的事情。

以下代码。有没有我遗漏的信息?

编辑:添加了授权地址代码。

Code
      (In HttpModule)
    private void BeginRequest(object Sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        var Id = new TASBIdentity();
        context.User = new TASBPrincipal(Id);
        Thread.CurrentPrincipal = context.User;
                      (etc...)

    (In global.asax)
    void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        var user = context.User;  // user is correct type
    }

    void Application_EndRequest(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        var user = context.User;    // user is default type (not correct)
    }

   (In the authorizeattribute)
public class SecuredByFunctionAttribute : AuthorizeAttribute
{
    private readonly string _functionKey;

    public SecuredByFunctionAttribute(string functionKey)
    {
        _functionKey = functionKey;
    }
    protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
    {
        return httpContext.User.IsInRole(_functionKey);
    }

0 个答案:

没有答案
相关问题