Home > front end >  How do I check the user email part that will come from Session?
How do I check the user email part that will come from Session?

Time:12-30

Using session in my program, I get the e-mail address and make transactions accordingly. If the user is not logged in, I want to return an error, but I cannot check the session information for null. I need help on this.

  if (string.IsNullOrEmpty(HttpContext.Session.GetString("userId").ToString()))
            {
                return RedirectToAction("ErrorPage");
            }
            string UserMail = HttpContext.Session.GetString("userId").ToString();

Error : Error

Error2: Error2

I tried to check the mail from session with if but it didn't work.

CodePudding user response:

you should not use ToString() here, just check for null.

if (HttpContext.Session.GetString("userId") == null)
{
    return RedirectToAction("ErrorPage");
}
string UserMail = HttpContext.Session.GetString("userId");

ToString() throws an exception when yo do something like the below.

string str = null;
str.ToString();
  • Related