Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 4-在beginrequest和endrequest之间丢失自定义主体_C#_Asp.net Mvc_Asp.net Mvc 4_Httpcontext - Fatal编程技术网

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

C# MVC 4-在beginrequest和endrequest之间丢失自定义主体,c#,asp.net-mvc,asp.net-mvc-4,httpcontext,C#,Asp.net Mvc,Asp.net Mvc 4,Httpcontext,在我的项目(c#)中,我们有一个HttpModule,它创建一个自定义主体,并将其附加到CurrentPrincipal 这适用于我们所有的MVC3应用程序和经典的ASP.Net应用程序 我们有一个用于保护控制器方法的AuthorizeAttribute覆盖-看起来相当标准 问题在于,在CustomAuthorizeAttribute中,用户(httpContext.user)是RolePrincipal,而不是自定义主体 为了排除故障,我在global.asax中放置了一些处理程序来捕获beg

在我的项目(c#)中,我们有一个HttpModule,它创建一个自定义主体,并将其附加到CurrentPrincipal

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

我们有一个用于保护控制器方法的AuthorizeAttribute覆盖-看起来相当标准

问题在于,在CustomAuthorizeAttribute中,用户(httpContext.user)是RolePrincipal,而不是自定义主体

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

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

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

代码如下。有什么我遗漏的信息吗

编辑:添加了authorizationattribute代码

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);
    }

这个问题有用吗?是的,我在我的web.config中删除了角色管理器,一切又恢复正常了。非常感谢。顺便说一句,查看正在运行的模块列表的代码也很酷。将其移动到您创建的答案中没有问题。