• 0

Making authenticated WebRequest in C#


Question

Hi there,

I'm trying to make a WebRequest in C# to a website that requires credentials.

Using the username and password in IE works, but doing it through code doesn't.

Maybe I have some misplaced code?

public bool WebRequestMethod(string uri) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(securelyStoredUserName, securelyStoredPassword);
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;

HttpWebResponse response = (HttpWebResponse) request.GetResponse();

return false;
}

Thanks,

David.

Link to comment
https://www.neowin.net/forum/topic/813426-making-authenticated-webrequest-in-c/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

I think you might find your answer in the Community Content section of the following MSDN Entry: http://msdn.microsoft.com/en-us/library/sy...ationlevel.aspx

  Quote
- When you set the WebRequest.AuthenticationLevel = AuthenticationLevel.MutualAuthRequired property, you will end up seeing the exception on the client which says:

System.Net.WebException: The request was canceled

---> System.Net.ProtocolViolationException: The requirement for mutual authentication was not met by the remote server.

- However, if you take a System.Net/ Network trace of the request what you will end up seeing is that the client sends the entire request (eg: POST with the entity body), the server processes the entire request and sends a response back to the client and only then, the client gets the exception.

- This behavior is by design and is the correct behavior. The reason being: the client cannot guarantee that the mutual authentication has been successful until it receives a "full" response from the server. The client can make a second call to InitializeSecurityContext only after it receives a WWW-Authenticate header with the server's Kerberos token. Only after this happens, it can call QueryContextAttributes to actually see if mutual authentication was successful.

- Since the server can respond back with a "full response" only after the entire request is complete, the only way the client can guarantee this is after getting a status code of (4xx or 2xx) from the server; which happens after sending the entity body.

- If you want to guarantee that the client & server mutually authenticate with each other, then you can send a dummy/ fake HEAD/ GET request to the server. If there is no mutual Authentication (the client and server talking anything but Kerberos), the HEAD/ GET request will fail with the expected Exception.

- You can then decide whether or not to send further POST requests.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • You invoke the game bar and then can pin it with transparent background.
    • 100% agree with this " As for real framerate vs framegen framerate, I don't think they exactly want you to know, given that their marketing has been strongly focused on hiding the real framerate and pretending the generated one is all that matters..."
    • I also use the classic Start menu in Open Shell in Windows 10/11
    • However, you would have to invoke it with the Windows key + G; in other words, it's useless. It is Microsoft's logic of placing things, and that is a company who have no idea how to design anything.
    • Samsung 2TB 9100 PRO, 990 PRO NVMe SSDs have never been cheaper by Sayan Sen It looks like the NVMe SSD market is once again coming to life if you are out there hunting for deals. Samsung, for example, is offering its 9100 PRO and 990 PRO drives at great prices that you should absolutely look at if you want to save some major bucks. The discounts apply on 2TB models (purchase links down below). First up, we have the 9100 PRO. It is based on PCIe Gen5 and thus promises double the speed of the 990 PRO. Samsung claims to deliver sequential read and write speeds of up to 14,700 MB/s and 13,400 MB/s, respectively, on the 2TB model. In terms of random throughput, the claimed numbers for the 2TB SKU is 1850K IOPS for the reads and 2600K IOPS for writes. The on-board DRAM cache helps in this regard with 2GB of LPDDR4X on the 2TB. The SSD is built on Samsung's in-house 8th Gen TLC V-NAND flash memory and has endurance ratings of 1200 TBW (terabytes written). Gen5 SSDs are known to run quite hot and thus the included SSDs are a must. You may even upgrade to something even better if you are having trouble managing the heat. Speaking of heat, Samsung says that the drives have an operating temperature of 0 to 70 Celsius, and upon exceeding this temperature, the drive's controller will begin throttling quite heavily. Get it at the link below: SAMSUNG SSD 9100 PRO without Heatsink 2TB, PCIe 5.0x4 M.2 2280 (MZ-VAP2T0CW): $239.99 (Amazon US) || $239.99 (Shipped and Sold by Newegg US) 990 PRO is, on the other hand, the fastest Gen4 drive from Samsung. There are some obvious differences like lower speeds on this compared to the 9100 PRO. Sequential reads and writes are rated at 7450 MB/s and 6900 MB/s respectively. The random throughputs for reads and writes are 1400K IOPS and 1550K IOPS respectively. The 990 PRO is based on Samsung's 7th Gen V-NAND flash and it too is TLC. The endurance rating for this is the same as that of the 9100 PRO. Get it at the link below: Samsung 990 PRO SSD NVMe M.2 PCIe Gen4, M.2 2280, MZ-V9P2T0B/AM: $149.99 (Shipped and Sold Amazon US) || $149.99 (Shipped and Sold by Newegg US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • First Post
      Thornskade earned a badge
      First Post
    • Week One Done
      Higante88 earned a badge
      Week One Done
    • Conversation Starter
      CarloDuplessis earned a badge
      Conversation Starter
    • First Post
      hhgygy earned a badge
      First Post
    • Collaborator
      WiltshireHam went up a rank
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      719
    2. 2
      ATLien_0
      278
    3. 3
      Michael Scrip
      204
    4. 4
      +FloatingFatMan
      190
    5. 5
      Steven P.
      129
  • Tell a friend

    Love Neowin? Tell a friend!