The following code is being used in ASP.NET MVC 5 project. Everytime I run the following code the ApplicationSignInManager Class always comes to be null causing null reference exception. Being fairly a newbie I don't understand what code calls in the constructor of AccountController class passing in the instance of usermanager and sign in manager. perhaps that is where I need to focus but the truth is I can't find that part of the code. Anyone up for help?
To be precise the exception is thrown from the HTTPost login method. The signInManager is always null.
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password,
model.RememberMe, shouldLockout: false);
Below is the c sharp code
[Authorize]
public class AccountController : Controller
{
private ApplicationUserManager _userManager;
private ApplicationSignInManager _signInManager;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set { _signInManager = value; }
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
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.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
}
Code from IdentityConfig.
// Configure the application sign-in manager which is used in this application.
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);
}
}
Check for the following inside your Identity.Config.cs
:
public static void RegisterAuth(IAppBuilder app)
{
// other code
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// other code
}
and somewhere in your project (possibly Startup.cs
)
[assembly: OwinStartup(typeof(Startup), "Configuration")]
namespace YourNamespace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
AuthConfig.RegisterAuth(app);
}
}
}
if that variable is always nulla maybe there is a problem with
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set { _signInManager = value; }
}
the first ime _signInManager
is null so if HttpContext.GetOwinContext().Get<ApplicationSignInManager>()
returns null your property will be null and you'll get that error.
Check what HttpContext.GetOwinContext().Get<ApplicationSignInManager>()
returns and be sure that it is a value different from null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With