0

I have the following C# code I'm trying to use to get a response from a url I can go to manually.

        static void Main(string[] args)
        {
            Uri theRequestUriString = new Uri("<<<my_url>>>");
            var request = WebRequest.Create(theRequestUriString);
            request.UseDefaultCredentials = true;
            WebProxy myproxy = new WebProxy("<<<my_proxy>>>:<<<my_port>>>", true);
            request.Proxy = myproxy;
            request.Timeout = Convert.ToInt32(TimeSpan.FromMinutes(5).TotalMilliseconds);
            request.Credentials = CredentialCache.DefaultCredentials;

            string textResponse;
            var resp = request.GetResponse();//*****401 error here*****
            using (var sr = new StreamReader(resp.GetResponseStream()))
            {
                textResponse = sr.ReadToEnd().Trim();
            }

        }
System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.'

I can go there manually but get a 401 Unauthorized if I try to use this code. Shouldn't the CredentialCache take care of that? I'm using Visual Studio locally if that is not the case.

If the url is instead https://www.yahoo.com then this works just fine.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Mark McGown
  • 975
  • 1
  • 10
  • 26
  • 3
    First rule of "*why isn't my http request working*" questions. Get it working in Postman first. Second rule, then use Ffiddler to work out the difference between postman and C# code – TheGeneral Feb 02 '21 at 03:58
  • 401 is a server response. Something about your request doesn't satisfy the server's requirements so it deems you not to be authorised. To further exapnd on why we can't just give you a solution: I could write a server application with something like this: `if (today == Friday && (priceOfCookiesAtStarBucks > 100 || weather == Rainy)) { return 401; }`. If you called my API, you would get a 401 response if those conditions were met. – ProgrammingLlama Feb 02 '21 at 04:16
  • Cookies should never be so expensive but I really like the example. – Mark McGown Feb 02 '21 at 04:58

1 Answers1

0

In your code

request.Credentials = CredentialCache.DefaultCredentials

means you are using NTLM for auth.

Windows authentication (formerly named NTLM, and also referred to as Windows NT Challenge/Response authentication) is a secure form of authentication because the user name and password are hashed before being sent across the network. When you enable Windows authentication, the client browser sends a strongly hashed version of the password in a cryptographic exchange with your Web server.

Read more here

This is basically your windows credentials. When one opens the website in the browser they are able to easily authenticate with the website using their windows credentials which is passed on to the website.

Now in regards to why it is not working when you run your code. The place where I would see is whether you are setting your content to use windows auth or not. There are several ways of doing it and some of them are here

I will also point you to this SO Answer: Windows authentication doesn't work when I run project from Visual Studio

Alok
  • 1,290
  • 1
  • 11
  • 21