Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get username in constructor for controller?

Tags:

asp.net-mvc

I have a controller that gets data based on current user that is logged in. And I would like to assign a local variable like this:

    public UsergroupsCustAdminController()
    {

        User u = _us.GetUsers(HttpContext.User.Identity.Name).First();

        this._customerID = u.CustomerID;
    }

Somehow I cant get the value for current user logged in. How should I fix this? I need to use that "CustomerID" in lots of places in my controllerclass.

/M

like image 414
Lasse Edsvik Avatar asked Feb 04 '10 08:02

Lasse Edsvik


1 Answers

You are trying to hook in too early. Suggest you override OnActionExecuting and put this code in there, like this:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    User u = _us.GetUsers(HttpContext.User.Identity.Name).First();
    this._customerID = u.CustomerID;
}
like image 169
David M Avatar answered Oct 10 '22 07:10

David M