accessing HttpContext.Request in a controller's constructor

前端 未结 4 1606
离开以前
离开以前 2020-12-12 20:30

I\'m following this ASP.NET MVC tutorial from Microsoft:

My code is slightly different, where I\'m trying to access HttpContext.Request.IsAuthenticated

4条回答
  •  既然无缘
    2020-12-12 21:15

    The solution of this problem is to create an override method of Initialize by passing RequestContext object.

    public class ChartsController : Controller
    {
         bool isAuthed = false;
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
    
            if (requestContext.HttpContext.User.Identity.IsAuthenticated)
            {
              isAuthed =true;
            }
        }
    }
    

提交回复
热议问题