views:

34

answers:

2

In order to avoid having to pass Data that goes in the master page of my site before every view in every controller I have created an ApplicationController that sets the data on its constructor... the problem with this approach is that one of the viewdatas I must pass is the url of the profile Image of the current logged in user.

[Authorize(Roles = "Administrator")]
    public abstract class AdministratorController : Controller
    {
        private IPortalAdministratorServices _servicioPortalAdministrator;
        public AdministratorController()
        {
            _servicioPortalAdministrator = new PortalAdministratorServices();
            ViewData["associates"] = _servicioPortalAdministrator.getAssociates();
            ViewData["picture"] = _servicioPortalAdministrator.GetPic();        

        public AdministratorController(IPortalAdministratorServices service)
        {
            _servicioPortalAdministrator = service;
            }

    }

and so my companies Controllers wich inherits from AdministratorController now doesnt have to set all that Data upon every View Call.

[Authorize(Roles = "Administrator")]
public class CompaniesController : AdministratorController
{
    private ICompaniasServices _service;

    public CompaniesController()
    {
        _service = new CompaniasServices(new ModelStateWrapper(this.ModelState));

    }

    public CompaniesController(ICompaniasServices service)
    {
        _service = service;
    }

The problem is this:

When I try to manually access the Companies Controller without actually being logged in the method GetPic() wont actually be able to get the picture url cause no one is logged in.. and these method is getting called in spite of the Authorize attribute....so now, even after being logged in the ViewData for the picture url has been permanently set to "unknown.png"

Some of this code isnt mine.. I just discovered the bug but cant figure out how to fix it.

A: 

Does Authorize work anywhere in your site? If not, could it be that you missed the section in your web.config?

Wil
Yes, I have placed some break points and Im positive that authorize works.. its just that it only redirects after all of the constructor instructions are executed.
NachoF
A: 
if (User.Identity.IsAuthenticated){
   ViewData["picture"] = _servicioPortalAdministrator.GetPic(); 
}
else{
   ...
}

Is this something u can use?

larole