Creating and accessing Cookies in ASP.NET MVC3

后端 未结 1 606
南笙
南笙 2020-12-28 21:57

How can I create cookies in the controller and access it in any view just like for example

User.Identity.Name

I can use that code anywhere

相关标签:
1条回答
  • 2020-12-28 22:31
    ....    
    //create cookie
    var cookie = new HttpCookie("cookieName");
    
    cookie.Value = "value";
    Response.Cookies.Add(cookie);
    
    //remove cookie
    var cookie = new HttpCookie("cookieName");
    cookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(cookie);
    
    //To Request the cookies value
    var val = Request.Cookies["cookieName"].Value;
    ....
    
    0 讨论(0)
提交回复
热议问题