Home > Software design >  How to use C# HTTPClient with Windows Credentials
How to use C# HTTPClient with Windows Credentials

Time:01-27

I wrote a console app that will read a text file of links and test them using the HttpClient class to check if the links exist. It works wonderfully at home when tested against common links such as google.com.

When I run the app at under my work intranet, though, I get "Forbidden" errors when it checks links on our company Sharepoint and "Unauthorized" errors when it checks links on the Azure Website. My hope was that running it under my authorized Windows desktop PC would be all I needed for it to work, but nope.

Any hints one how to pass my Network credentials when I access the links with HttpClient?

EDIT: Added code to handle passed console argument (string of authentication cookies)

using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

namespace linkbot
{
    class Program
    {   
        private static async Task ProcessRepositories(string the_url, string the_cookies)
        {   int the_index=the_url.IndexOf("/",9);
            string base_string=the_url;
            string the_rest="/";
            if (the_index>=0)
            {
                base_string=the_url.Substring(0,the_index);
                the_rest=the_url.Substring(the_index);
            }
            
            try {
                var baseAddress = new Uri(base_string);
                using (var handler = new HttpClientHandler { UseCookies = false })
                using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
                {
                    var message = new HttpRequestMessage(HttpMethod.Get, the_rest);
                    message.Headers.Add("Cookie", the_cookies);
                    var result = await client.SendAsync(message);
                    result.EnsureSuccessStatusCode();
                }
                Write("\n"   the_url   " - WORKED!");
            }
            catch(Exception e )
            {
                Write("\nFailed: "   the_url   "||||||"  e.ToString() );
                //throw e;
            }
        }
        static async Task Main(string[] args)
        {
            if (args.Length<2){
                Console.Write("\n###################\nLinkChecker by Sean J. Miller 2022\n\nusage:\n    linkchecker.exe <linkfile> <cookies>\nwhere <linkfile> contains a text file of fully specified links (URLs) with one link per line.  Example, https://www.google.com\n\nAn output file is generated titled brokenlinks.txt in the same directory where LinkChecker was launched.  <cookies> should be a string such as \"cookie1=value1;cookie2=value2;cookie3=value3;\"\n###################\n\n\n");
                return;
            }
            System.IO.File.Delete("brokenlinks.txt");
            System.IO.File.WriteAllText("brokenlinks.txt", "Last Check Started "   (DateTime.Now).ToString());
            int counter=0;
            int total_lines=TotalLines(@args[0]);
            Console.Write("Started "   (DateTime.Now).ToString()   "\n");
            foreach (string line in System.IO.File.ReadLines(@args[0]))
            {
                Console.Write("Processing Link "   (  counter)   "/"   total_lines   "\n");
                await ProcessRepositories(line, args[1]);
            }
            Console.Write("Finished "   (DateTime.Now).ToString()   "\n");
            Write("\n");
        }

        static void Write(string the_text)
        {
            //Console.Write(the_text);
            try   
            {  
                System.IO.File.AppendAllText("brokenlinks.txt", the_text);
            }
            catch (Exception ex)   
            {  
                throw ex;  
            }
        }

        static int TotalLines(string filePath)
        {
            using (StreamReader r = new StreamReader(filePath))
            {
                int i = 0;
                while (r.ReadLine() != null) { i  ; }
                return i;
            }
        }
    }
}

CodePudding user response:

You want to set UseDefaultCredentials to true to use the current logged-on user credentials in your request. You can do that by instantiating your HttpClient like so:

private static readonly HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
  •  Tags:  
  • Related