views:

2523

answers:

6

Hi folks

As described in this post, I created an abstract base controller class in order to be able to pass data from a controller to master.page. In this case, I want to lookup a user in my db, querying for User.Identity.Name (only if he is logged in).

However, I noticed that in this abstract base class the User property is always null. What do I have to do to get this working?

Thanks a lot

+1  A: 

to use the user, you should get the current page from HttpContext.Current Try something like that:

HttpContext.Current.User.Identity.Name

hope it helps..

Adam Right
that doesn't work
craigmoliver
A: 

Nope, that didn't help unfortunately. Maybe I should provide a code sample illustrating what I'm trying to do:

public abstract class ApplicationController : Controller
    {
        private IUserRepository _repUser;

        public ApplicationController()
        {
            _repUser = RepositoryFactory.getUserRepository();
            var loggedInUser = _repUser.FindById(User.Identity.Name);
            ViewData["LoggedInUser"] = loggedInUser;
        }
    }

I am setting the User.Identity.Name via FormsAuthentication at another place in the code, but I think this can't be the problem - afaik User.Identity.Name can be null, but not User itself.

The problem is that I can't even call

User.Identity.Name

because User is already null. Seems like the HttpContext is not available in my Application Controller, as opposed to my deriving controllers.

I think the solution must be quite simple as this is an approach described in the official MVC tutorials, only they didn't have to access User.Identity.Name at this point.

Ideas, anyone? Thanks so far.

Masterfu
You can edit your question.
Paco
It might be null because the viewdata is initialized after the constructor
Paco
I wouldn't go for this approach anyway, because it's violating the single responsibility principle. Is a controller responsible for providing information about who is logged in?
Paco
A: 

I use Page class on my static Utlilites classes. Like that;

Page P = (Page)HttpContext.Current.Handler;

and i can get all properties via the P object for the current requested page..

Adam Right
A: 

Have you tried this: ControllerContext.HttpContext.Current.User.Identity.Name?

A: 

By setting authentication to Windows in web.config, you can get the user with User.Identity.Name

Danny
+3  A: 

As Paco suggested, the viewdata isn't initialized till after you are trying to use it.

Try overriding Controller.Initialize() instead:

public abstract class ApplicationController : Controller
{
    private IUserRepository _repUser;

    public ApplicationController()
    {
    }

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        base.Initialize(requestContext);

        _repUser = RepositoryFactory.getUserRepository();
        var loggedInUser = _repUser.FindById(User.Identity.Name);
        ViewData["LoggedInUser"] = loggedInUser;
    }
}
Melethril