HOW TO SET COOKIE VALUE IN C#?

john rob

Total Post:108

Points:756
Posted by  john rob
C#  Cookies 
C#
 644  View(s)
Ratings:
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();
  Response.Cookies.Add(userinfoid);                
  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.
  1. Tarun Kumar

    Post:214

    Points:1642
    Re: How to set cookie value in C#?

    You can set the cookie value like this:
    Response.SetCookie()
    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!