Home > DeveloperSection > Forums > How to set cookie value in C#?
john rob

Total Post:108

Posted on    June-16-2016 1:44 AM

 C# C#  Cookies 

 1 Reply(s)
 590  View(s)
Rate this:
I am getting problem to set cookie value, my code is below:
public ActionResult sellerLogin(string userid,string password)
int count = db.userinfo.Count(p => p.userId == userid && p.password == password);
if (count == 1)
  HttpCookie userinfoid = new HttpCookie("userinfoid");
  userinfoid["uerinfoid"] = userid.ToString();
  return View("loginSuccess");
return View();

In the above code I have set the value in the cookie but when I access that value in the viewbag from another view controller I can't find that value.
Please help me, how to set or get the cookie value.

Tarun Kumar

Total Post:214

Posted on    June-16-2016 11:57 PM

You can set the cookie value like this:
because Response.Cookie.Add() is use to add multiple cookies whereas SetCookie will update an existing cookie.

Or, you can add logic that will check the cookie is already exists or not if yes then use Response.SetCookie else Response.Cookie.Add.
I hope it will be helpful for you.

Modified On Jun-16-2016 11:57:29 PM

Don't want to miss updates? Please click the below button!

Follow MindStick