Home > Back-end >  Session Value Mismatch in ASP.Net Core MVC
Session Value Mismatch in ASP.Net Core MVC

Time:03-01

After login to the application, we are setting a value in a session variable in the Login Controller as below:

public ActionResult Login(LoginInfo loginInfo, string returnUrl)
{
    //...
    HttpContext.Session.SetString(SessionKeyPID, 226617); //PersonId - 226617
}

But later when we try to get the same in another controller with below:

[HttpGet]        
public JsonResult GetDetails()
{
    int person = Convert.ToInt32(HttpContext.Session.GetInt32(SessionKeyPID));
}

I'm getting different value like 842151478 instead of having 226617. Why I'm not getting same value. Can I get the same value?

CodePudding user response:

Change

int person = Convert.ToInt32(HttpContext.Session.GetInt32(SessionKeyPID));

to

int person = Convert.ToInt32(HttpContext.Session.GetString(SessionKeyPID));

Also you need to set the session key-value as a string, like:

// 226617 --> "226617"
HttpContext.Session.SetString(SessionKeyPID, "226617");
  • Related