views:

312

answers:

3

For my actions that are going to interact with the User's account, I would like to create a "TheUser" object in addition to adding that object to "ViewData["TheUser"]" as soon as any action on my controller is called.

If the User is logged in, it will grab the User's info from the database, if not, "TheUser" object will just be null.

I tried accessing "User.Identity.Name" in the controller constructor, but it isn't created prior to any action being called.

I was looking at custom authorization filters, but those wouldn't allow me to create the "TheUser" object and store it in the ViewData.

This is a brief snippet of what I would like to accomplish:

[Authorize]
public class HomeController : Controller
{
    User TheUser;

    public HomeController()
    {
        TheUser = User.Identity.IsAuthenticated ? UserRepository.GetUser(User.Identity.Name) : null;

        ViewData["TheUser"] = TheUser;  
    }
}
+1  A: 

Does the User have to be instantiated for every Controller action or just specific ones?

If you create an ActionFilterAttribute, you do have access to the Controller context. Not sure if that is true for AuthorizationFilters, but you could try something like this:

public class MyCustomFilter: ActionFilterAttribute
{

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["TheUser"] = User.Identity.IsAuthenticated ? UserRepository.GetUser(User.Identity.Name) : null;
        base.OnActionExecuting(filterContext);
    }
}

Then, attach this to the necessary controller actions.

JoshJordan
TheUser It is used for every action. What puzzles me is that although [Authorize] ensures the user is authenticated, I don't have access to User.Identity, but I can access ViewData.
Baddie
A: 

I created a custom base Controller class and added a "TheUser" property to it.

In the Execute method, I placed the logic to get the user and then add it to the ViewData and the "TheUser" property of the controller.

I had my controllers inherit the custom base Controller class and I was able to reference "TheUser" variable anywhere in the controller:

public class CustomControllerClass : Controller
{
    public User TheUser;

    protected override void Execute(System.Web.Routing.RequestContext requestContext)
    {
         if (requestContext.HttpContext.User.Identity.IsAuthenticated)
        {
            TheUser = UserRepository.GetUser(requestContext.HttpContext.User.Identity.Name);
            ViewData["TheUser"] = TheUser;
        }
        else
            ViewData["TheUser"] = null;

    }
Baddie
A: 

I setup my base controller in a similar way unsing using the constuctor method, and I was able to read this User info on the following mannner. Note: I only store the UserId, not the User object, but the point of my code is to show how you can get the required info in the contriuctor method.

public abstract class BaseController : Controller
{
    int _UserId = 0;

    public int UserId
    {
        get { return _UserId; }
        set { _UserId = value; }
    }

    public BaseController()
    {
        var userFromAuthCookie = System.Threading.Thread.CurrentPrincipal;

        if (userFromAuthCookie != null  && userFromAuthCookie.Identity.IsAuthenticated) // && !String.IsNullOrEmpty(userFromAuthCookie.Identity.Name))
        {
            busUser userBO = AceFactory.GetUser();
            string userNameFromAuthCookie = userFromAuthCookie.Identity.Name;
            _UserId = userBO.GetUserIdByUsername(userNameFromAuthCookie);
        }
    }
MattSlay