首页 > 解决方案 > SignInManager.PasswordSignInAsync keep return me null object refrence

问题描述

in my controller i use model to login these work fine and the model is give me the correct data when i log in to the account gives me null object reference

these is my model

    public class LoginViewModel
    {
        [Required]
        //[Display(Name = "Email")]

        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

and my action here

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

public async Task<ActionResult> LoginUser(LoginViewModel model, string returnUrl)
       {
           if (!ModelState.IsValid)
           {
               return View(model);
           }
           var user = UserManager.FindByName(model.UserName);

           var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
           switch (result)
           {
               case SignInStatus.Success:
                   return RedirectToLocal(returnUrl);
               case SignInStatus.LockedOut:
                   return View("Lockout");
               case SignInStatus.Failure:
               default:
                   ModelState.AddModelError("", "Invalid login attempt.");
                   return View(model);
           }
       }

and here is the configration

 public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
    {
        public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
            : base(userManager, authenticationManager)
        {
        }

        public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
        {
            return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
        }
        public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
        {
            var d = new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
            return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        }
    }

and here the error in the browser

and these my startup class that is which i configure the identity

[assembly: OwinStartup(typeof(Baity.Startup))]

namespace Baity
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {

           ConfigureAuth(app);

        }

    }
}


标签: c#asp.netasp.net-mvcasp.net-web-api

解决方案


Since the following line works

var user = UserManager.FindByName(model.UserName);

Then it means that the SignInManager is not initialized in your controller.

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);

If you share your initialization code for the SignInManager we can help you more.

To get the manager in Identity.Config.cs

public static void RegisterAuth(IAppBuilder app)
{
   // Initialize the creation
   app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

}

And in startup.cs

[assembly: OwinStartup(typeof(Startup), "Configuration")]    
namespace MyNamespace
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            AuthConfig.RegisterAuth(app);
        }
    }
}

推荐阅读