Question

I have a problem implementing forms authentication with an IOC container in my ASP.NET MVC 3 project. We have stored our user information in the database and has a lot of custom properties.

I have an interface of my user definition registrated to the IOC container for development purposes. This interface is given to each controller so the controllers has current user information.

This al works fine until i remove the dummy user registration in the Application_Start

I receive this error: The current type, ...CurrentUserInformation.IUserInformation, is an interface and cannot be constructed. Are you missing a type mapping?

I don't want to work with a dummy user object because I think this is not the best practice.

Can sombody help me or is there a better way to do this custom authentication?

edit added some code

BaseController

public class BaseController : Controller
{
   private readonly IUserInformation _userInformation;
   public BaseController(IUserInformation userInformation)
   {
       _userInformation = userInformation
   }
}

Bootstrapper Initialize called from Application_Start

public static void Initialise()
{
    var container = BuildUnityContainer();
    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

private static IUnityContainer BuildUnityContainer()
{
    var container = new UnityContainer();

    //register all services en repositories

    //here i put my dummy user wich i want to remove
    container.RegisterInstance<IUserInformation>(
    new UserInformation
    {
        UserId = 1,
        ... 
    }); 


    return container;
}
Was it helpful?

Solution

You can use InjectionFactory:

container.RegisterType<IUserInformation, UserInformation>(

    // User information is destroyed when the request ends.
    //   You could use an HttpSessionLifetimeManager as well, if it fits your needs
    new HttpRequestLifetimeManager(), 


    new InjectionFactory(container => { 
          UserInformation userInfo = // TODO: build your userInformation from custom authentication
          return userInfo;
    })); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top