A cookie is controlled by some attribute set in the cookie header, these attributes are as follows:
Domain: It is the specified domain that is receives the cookie. If the domain is not specified then the domain is the origin server.
Path: It specifies the limit in the domain if the path is not specify then it uses the URI path.
Expires: It specifies the date/time when the cookie will be expire, the user can delete the cookie when it has expired.
Max-age: It is a numeric value that specifies the timespan of the cookie that includes the life of the cookie. It can be deleted if it reaches its maximum age like as "expires" does.
Web API and Cookies
We know that the Web API creates services over the HTTP. It can be a web page for making AJAX requests, it can be a native app for retrieving the data or it can be a headless bot for pooling the data. The Web API does the work over the HTTP and the cookie is the part of the HTTP.
Setting a Cookie with multiple values
public HttpResponseMessage Get()
{
HttpResponseMessage respMessage = new HttpResponseMessage();
respMessage.Content = new ObjectContent(new string[]
{
"value1",
"value2"
}, new JsonMediaTypeFormatter());
var nvc = new NameValueCollection();
nvc["sessid"] = "1234";
nvc["3dstyle"] = "flat";
nvc["theme"] = "red";
var cookie = new CookieHeaderValue("session", nv);
cookie.Expires = DateTimeOffset.Now.AddDays(1);
cookie.Domain = Request.RequestUri.Host;
cookie.Path = "/";
respMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie });
return respMessage;
}
https://www.devcurry.com/2013/04/http-cookies-and-aspnet-web-api.html
Domain: It is the specified domain that is receives the cookie. If the domain is not specified then the domain is the origin server.
Path: It specifies the limit in the domain if the path is not specify then it uses the URI path.
Expires: It specifies the date/time when the cookie will be expire, the user can delete the cookie when it has expired.
Max-age: It is a numeric value that specifies the timespan of the cookie that includes the life of the cookie. It can be deleted if it reaches its maximum age like as "expires" does.
Web API and Cookies
We know that the Web API creates services over the HTTP. It can be a web page for making AJAX requests, it can be a native app for retrieving the data or it can be a headless bot for pooling the data. The Web API does the work over the HTTP and the cookie is the part of the HTTP.
Setting a Cookie with multiple values
public HttpResponseMessage Get()
{
HttpResponseMessage respMessage = new HttpResponseMessage();
respMessage.Content = new ObjectContent
{
"value1",
"value2"
}, new JsonMediaTypeFormatter());
var nvc = new NameValueCollection();
nvc["sessid"] = "1234";
nvc["3dstyle"] = "flat";
nvc["theme"] = "red";
var cookie = new CookieHeaderValue("session", nv);
cookie.Expires = DateTimeOffset.Now.AddDays(1);
cookie.Domain = Request.RequestUri.Host;
cookie.Path = "/";
respMessage.Headers.AddCookies(new CookieHeaderValue[] { cookie });
return respMessage;
}
https://www.devcurry.com/2013/04/http-cookies-and-aspnet-web-api.html
No comments:
Post a Comment