Setting Cookies in ASP.NET Web Interface

I have a web application that creates two cookies in host pid and tid. Pid has a value created on the node, and tid has a value of "0".

My requirement is that when the host calls the MVC Web API 2 server, it sends these cookies, if the tid value is "0", create a guid value for it and add the cookie expiration date in a day. If cookie tid has a value other than "0", simply increase the cookie expiration date.

[HttpGet]
public HttpResponseMessage check()
{
    var resp = new HttpResponseMessage(HttpStatusCode.OK);
    CookieHeaderValue cook = Request.Headers.GetCookies("tid").FirstOrDefault();
    if (cook["tid"].Value != "0")
    {
        var cooki = new CookieHeaderValue("tid", Guid.NewGuid().ToString());
        cooki.Expires = DateTimeOffset.Now.AddDays(1);
        cooki.Domain = Request.RequestUri.Host;
        cooki.Path = "/";
        resp.Headers.AddCookies(new CookieHeaderValue[] { cooki });
    }
    return resp;
}

HTTP cookie tid ( , , cookie tid ), cookie tid , , , , cookie tid, "0" .

- ? , - ?

:

if (cook["tid"].Value != "0")
{
    cook["tid"].Value = Guid.NewGuid().ToString();
    cook.Expires = DateTimeOffset.Now.AddDays(1);
    cook.Domain = Request.RequestUri.Host;
    cook.Path = "/";
    resp.Headers.AddCookies(new CookieHeaderValue[] { cook });
}        
return resp;

, , . cook["tid"].Value guid.

cookie "cook", cookie (tid) "0" , , pid tid .

+4

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


All Articles