How to get the value of a cookie from a specific domain in .NET 6?

Viewed 29

I have to authenticate myself on an external site, get the key value (GLBID) that is registered in a cookie and use it to access the endpoints that need authentication.


FrontEnd

<iframe src="https://login.globo.com/login/438?url=http://globoesporte.globo.com/cartola-fc/&amp;tam=WIDGET" style="height: 100%; width : 100%;"></iframe>

After authenticating, the value is recorded as below:

Cookie Record

Using the value collected in the header of my http request (the key becomes X-GLB-Token):

Endpoint

1 Answers

you can use CookieContainer and check domain:

var request = (HttpWebRequest)WebRequest.Create(args[0]);
request.CookieContainer = new CookieContainer();

using (var response = (HttpWebResponse) request.GetResponse())
{
    // Print the properties of each cookie.
    foreach (Cookie cook in response.Cookies)
    {
    if(cook.Domain == "your Domain")
       {
         //do something
       }
    }
}
Related