My code is simple like :
[HttpGet]
public ActionResult Login ()
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Home");
}
return View(new LoginModel());
}
[HttpPost]
public ActionReslt Login (LoginModel model)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.Email, model.Password))
{
FormsAuthentication.SetAuthCookie(model.Email, false);
return RedirectToAction("Index","Home");
}
}
return View(model);
}
But my Log Service sometimes receive this error! System.NullReferenceException: Object reference not set to an instance of an object
I don't think that the best practice to do null checks for every reference like :
if (User!=null && User.Identity!=null && User.Identity.IsAuthenticated)
Could you tell me why sometimes it is null ? and sometimes not ? What is the best practice for it?