Redeem Cookie Values

Asked

Viewed 109 times

0

Good afternoon I have an application in MVC that generates some cookies, I wonder if it is possible to redeem these cookies through an Api, because I tried and unfortunately could not.

Code generated for the api:

[Route("ResgataCookie")]
[HttpGet]
public IHttpActionResult ResgataCookie()
{
    string test = "";
    if (HttpContext.Current.Request.Cookies["RMACookies"] != null)
    {
        test = HttpContext.Current.Request.Cookies["RMACookies"].Value;
    }

    return Ok(test);
}

Following image below: inserir a descrição da imagem aqui

1 answer

0

Cookies in the Webapi can be accessed using Request.Headers.GetCookies

See the documentation on https://docs.microsoft.com/pt-br/aspnet/web-api/overview/advanced/http-cookies

Below is an example

[HttpGet]
[Route("ResgataCookie")]
public IHttpActionResult ResgataCookie()
{
  var cookies = Request.Headers.GetCookies().FirstOrDefault();

  if (cookies == null || !cookies.Cookies.Any(p => p.Name == "RMACookies"))
    return NotFound();

  var item = cookies.Cookies.First(p => p.Name == "RMACookies").Value;

  return Ok(item);
}
  • Hello friend I did that way, too, but the same returns me null. cookies are generated in a standard MVC application, but I am trying to access cookies through the api and unfortunately return blank.

  • Are you trying to access cookies from another application? Remembering that cookies are stored by domains and sub-domains so if they are different you won’t even have access.

  • Thank you very much Danilo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.