Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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);
                Could you find which variable is null and could you show your startup configureServices in which you register the identity ?stackoverflow.com/questions/55230614/…
– Ryan
                Feb 3, 2020 at 2:12

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);
                here is my initialization of sign in manager  ``` public ApplicationSignInManager SignInManager         {             get             {                 return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();             }             private set             {                 _signInManager = value;             }         }```
– Ahmed Mehany
                Feb 2, 2020 at 11:18
                Check stackoverflow.com/questions/23252251/… or share your startup file to check configuration.
– Athanasios Kataras
                Feb 2, 2020 at 11:38
                here is my startup class public partial class Startup     {         public void Configuration(IAppBuilder app)         {             ConfigureAuth(app);          }              }
– Ahmed Mehany
                Feb 2, 2020 at 11:49
                Have you added the Microsoft.Owin.Host.SystemWeb package and the assembly: OwinStartup(typeof(TestMVC5.Startup))] in your startup?
– Athanasios Kataras
                Feb 2, 2020 at 11:52

What I figure out was that _signInManager = signInManager; was missing in the constructor of the Controller class so double check that.

 private readonly SignInManager<ApplicationUser> _signInManager;
 public (AccountController(SignInManager<ApplicationUser> signInManager)
     //this was the issue as it was missing so add it should work 
       _signInManager = signInManager;

i found the answer for my question is that in the identity configration and that's why it was not working for me

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)
        return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.