VajNyiaj
VajNyiaj

Reputation: 1910

Missing HttpContext.Items for some actions

Background:

MVC3 intranet application using windows authentication. After windows authentication completes, a HttpModule looks up the user's network id from an HR database and returns the user's employee information and sets it in HttpContext.Items. I have a base controller that looks for this information and sets a ViewBag property by overriding OnActionExecuting.

My question is that this HttpContext.Items["UserInfo"] information only seems to be available on Home/Index only and not available when I click to Home/About or Home/Help although HomeController inherits BaseController. Can anyone shed light on why this is happening?

protected override void OnActionExecuting(ActionExecutingContext ctx)
        {          
            if (this.HttpContext.Items["UserInfo"] != null)
            {
                UserInfo User = (UserInfo)this.HttpContext.Items["UserInfo"];
                ViewBag.CurrentUser = User;
            }            

            base.OnActionExecuting(ctx);
        }

Upvotes: 0

Views: 551

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

HttpContext.Items is per request only; it is not retained when you redirect to another view or even post back within the current view. So you need to use Session or something else to persist it.

Upvotes: 1

Related Questions