2

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?

Mohamed Farrag
  • 1,614
  • 2
  • 19
  • 38
  • [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Nov 03 '14 at 15:17
  • `NullReferenceException` is a common situation for programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Nov 03 '14 at 15:18
  • 1
    He clearly knows what a NullReferenceException is and how to fix it. That's not really his question. – Jonesopolis Nov 03 '14 at 15:22
  • 1
    yes Soner as Jonesy says , I know The `NullReferenceException` but I'm wondering why it is sometimes throwing the exception and sometimes not, I can do the null checks but I'm using `User.Identity.IsAuthenticated` in many places and I don't want my code to be bad because of the null checks. And also it is always not null in debug mode so I never get this error. – Mohamed Farrag Nov 03 '14 at 15:28
  • 2
    @Figo, could you please show how do you authenticate your users? The problem might be there. Also you might be on a safe side using `Request.IsAuthenticated` instead of `User.Identity.IsAuthenticated`. – Andrei Nov 03 '14 at 15:32
  • @Andrei I've posted it. – Mohamed Farrag Nov 03 '14 at 15:50

1 Answers1

4

You should use Request.IsAuthenticated instead of User.Identity.IsAuthenticated

Page.User.Identity.IsAuthenticated returns object reference not set to instance of object

Internally Request.IsAuthenticated will verify that the User and it's Identity are set (not null). You could do the same in your code, but why bother.

User.Identity.IsAuthenticated is set when you invoke these methods :

FormsAuthentication.Authenticate(name, pwd)

Or

Membership.ValidateUser(name, pwd)

Or other Authentication mechanism methods that set cookies

Community
  • 1
  • 1
Joan Caron
  • 1,859
  • 16
  • 21
  • 2
    Thanks I think this is the right way and I also read about the difference between Request.IsAuthenticated and User.Identity.IsAuthenticated – Mohamed Farrag Nov 04 '14 at 08:21