Setting and receiving asp.net mvc4 cookies

I am trying to set and get cookies in a project that I am working on in asp.net mvc4.

This is how I set cookies:

                    var Username = new HttpCookie("Username");
                    Username.Value = model.UserName;
                    Username.Expires = DateTime.Now.AddDays(7);
                    Username.Secure = true;
                    Response.Cookies.Add(Username);

Then, in another controller action, I try to do this:

HttpCookie cookie = Request.Cookies["Username"];

but I get null for this particular cookie. In addition, I do not know if this will matter, but I do not ask for a cookie in the action in which this current action is redirected, but in other actions. This should not change as I set the expiration date to +7 days from the date of creation.

Each comment is rated.

Thanks in advance, Laziale

+4
source share
1 answer

Latsial

.

, cookie .

 Username.Secure = true;

. . Request.Cookie.

.

+1

Source: https://habr.com/ru/post/1531552/


All Articles