简体   繁体   English

UNITY MVC 容器在具有授权属性的帐户控制器中捕获空异常

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

I have a question.我有个问题。 I'm new to UnityContainer .我是UnityContainer I use Unity for DI.我使用 Unity 进行 DI。

My ASP.NET MVC web app has an AccountController (which was auto-generated by ASP.NET Identity) with the [Authorize] attribute.我的 ASP.NET MVC Web 应用程序有一个带有[Authorize]属性的AccountController (由 ASP.NET Identity 自动生成)。

[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查看我的完整代码:我的简单 AccountController 是由 VS-Identity 自动生成的

I try to inject constructor for AccountController .我尝试为AccountController注入构造函数。

My code is in App_Start > UnityConfig.cs :我的代码在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我的 UnityConfig

When I start to run the app, and call a method on the AccountController , I ge tthis error message:当我开始运行应用程序并调用AccountController上的方法时,我收到此错误消息:

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 Url 的 localhost:xxxxx/Account/Login

I found that when I removed [Authorize] and [ValidateAntiForgeryToken] attributes, everything worked well.我发现当我删除[Authorize][ValidateAntiForgeryToken]属性时,一切正常。

But when I added [Authorize] or [ValidateAntiForgeryToken] attributes, the exception was thrown.但是当我添加[Authorize][ValidateAntiForgeryToken]属性时,抛出了异常。

Please help me to fix it.请帮我修复它。

I really appreciate it!对此,我真的非常感激!

I found my answer.我找到了我的答案。 In Application_StartApplication_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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC5客户控制器空引用异常 - MVC5 Account Controller null reference exception 使用 Unity DI 的帐户控制器中的 IRepositories 为空 - IRepositories is null in account controller with Unity DI 使用 MVC controller 中的 [Authenticate] 属性使用会话授权用户? - Use [Authenticate] attribute in MVC controller using sessions to authorize users? MVC RoleProvider和Authorize属性 - MVC RoleProvider and Authorize attribute 在 Unity 中捕获所有异常 - Catch all exception in Unity 具有Authorize属性的控制器被调用两次 - Controller with Authorize attribute is called twice 来自区域中控制器的ASP.NET MVC [Authorize]在根文件夹中找不到Account / Login ActionResult - ASP.NET MVC [Authorize] from controller in area can't find Account/Login ActionResult in root folder try catch中的异常的message属性是否为空字符串或NULL? - Is the message attribute of an exception in try catch ever an empty string or NULL? “授权”属性与“角色”是否考虑了“索赔” - does Authorize attribute with Roles take Claims into account 如何解决应用了Authorize属性的MVC控制器的鸡蛋依赖性解析? - How to solve chicken-egg dependency resolution for MVC controller with Authorize attribute applied?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM