<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tiago Monteiro</title>
    <description>The latest articles on DEV Community by Tiago Monteiro (@dataknight1).</description>
    <link>https://dev.to/dataknight1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1351215%2F364e4514-c096-4c32-b432-ef58acdd4c6a.jpeg</url>
      <title>DEV Community: Tiago Monteiro</title>
      <link>https://dev.to/dataknight1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dataknight1"/>
    <language>en</language>
    <item>
      <title>XSRF-TOKEN</title>
      <dc:creator>Tiago Monteiro</dc:creator>
      <pubDate>Fri, 19 Apr 2024 11:21:48 +0000</pubDate>
      <link>https://dev.to/dataknight1/xsrf-token-hc9</link>
      <guid>https://dev.to/dataknight1/xsrf-token-hc9</guid>
      <description>&lt;p&gt;How is it possible to make http request for login in C#?&lt;br&gt;
When I try to make the http request with the POST protocol I cannot obtain the XSRF-TOKEN.&lt;br&gt;
Can anyone help me?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public class NetworkService : INetworkService
    {
        private readonly HttpClient _webClient;
        private readonly CookieContainer _cookieManager;
        private readonly HttpClientHandler _webHandler;

        public NetworkService(IHttpClientFactory clientFactory)
        {
            _cookieManager = new CookieContainer();
            _webHandler = new HttpClientHandler()
            {
                SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13,
                MaxAutomaticRedirections = 10,
                UseCookies = true,
                CookieContainer = _cookieManager,
                AllowAutoRedirect = true,
                CheckCertificateRevocationList = true
            };

            _webClient = clientFactory.CreateClient("NetworkServiceClient");
            _webClient.BaseAddress = new Uri("[URL]https://example.com/api[/URL]");
            _webClient.DefaultRequestHeaders.Accept.Clear();
            _webClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        public async Task&amp;lt;string&amp;gt; CallApi1(string userId, string accessCode)
        {
            var endpoint = new Uri(_webClient.BaseAddress, "/api1/login");
            var loginInfo = new { userName = username, userPassword = password };
            var contentBody = new StringContent(JsonConvert.SerializeObject(loginInfo), Encoding.UTF8, "application/json");

            HttpResponseMessage result = await _webClient.PostAsync(endpoint, contentBody);
            if (!result.IsSuccessStatusCode)
            {
                Console.WriteLine($"API1 call failed with status code: {result.StatusCode}");
                return null;
            }

            var token = ExtractCookie("XSRF-TOKEN");
            return token;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is functioning properly and returns a 200 response, however, I’m not receiving the XSRF Cookie. When I perform the HTTP request in Postman, I do receive the XSRF Cookie. But in C#, it’s not being received.&lt;/p&gt;

</description>
      <category>csharp</category>
    </item>
  </channel>
</rss>
