简体   繁体   中英

UNITY MVC CONTAINER CATCH NULL EXCEPTION IN ACCOUNT CONTROLLER WITH AUTHORIZE ATTRIBUTE

I have a question. I'm new to UnityContainer . I use Unity for DI.

My ASP.NET MVC web app has an AccountController (which was auto-generated by ASP.NET Identity) with the [Authorize] attribute.

[Authorize]
public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    [InjectionConstructor]
    public AccountController() { }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set
        {
            _signInManager = value;
        }
    }

    public ApplicationUserManager UserManager
    {
        get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
        private set { _userManager = value; }
    }

    [AllowAnonymous]
    public ActionResult Login(string returnUrl) { ViewBag.returnUrl = returnUrl; return View(); }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginViewModel model) 
    {
       //My code
       return RedictToAction("Index","Home");
    }
   
    //Some Methods
    
}

View my complete code: My simple AccountController was auto-generated by VS - Identity

I try to inject constructor for AccountController .

My code is in App_Start > UnityConfig.cs :

public static class UnityConfig
{
    public static IUnityContainer Container;

    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        //register all my components with the container here            

        container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
        container.RegisterType<DbContext, ApplicationDbContext>(new PerRequestLifetimeManager());
        container.RegisterType<UserManager<ApplicationUser>>();
        container.RegisterType<ApplicationUserManager>();
        container.RegisterType<AccountController>(new InjectionConstructor());            
        
        DependencyResolver.SetResolver(new Unity.AspNet.Mvc.UnityDependencyResolver(container));
    }
}

My UnityConfig

When I start to run the app, and call a method on the AccountController , I ge tthis error message:

Value cannot be null. Parameter name: container

Please click below link to view the error message:

The Exception was thrown. Url's localhost:xxxxx/Account/Login

I found that when I removed [Authorize] and [ValidateAntiForgeryToken] attributes, everything worked well.

But when I added [Authorize] or [ValidateAntiForgeryToken] attributes, the exception was thrown.

Please help me to fix it.

I really appreciate it!

I found my answer. In Application_Start

protected void Application_Start()
{
       AreaRegistration.RegisterAllAreas();
       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
       RouteConfig.RegisterRoutes(RouteTable.Routes);
       BundleConfig.RegisterBundles(BundleTable.Bundles);
       UnityConfig.RegisterComponents(); //<--- Incorrect
}

And I adjusted it

protected void Application_Start()
{
       AreaRegistration.RegisterAllAreas();
       UnityConfig.RegisterComponents(); //<========
       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
       RouteConfig.RegisterRoutes(RouteTable.Routes);
       BundleConfig.RegisterBundles(BundleTable.Bundles);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM