• 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

    • Thank you for mentioning Windows Vista in the title of a news article!
    • Let's be real. Part of the motivation of the UI change is to increase CPU/RAM usage, which slows down older devices and makes battery life worse, therefore tempting people into upgrading to the latest devices. We've been through this enough times to know that the iOS updates never improve performance. Personally I value battery life over eye candy.
    • Apple announces watchOS 26 with a 'Workout Buddy,' new gestures, and more by Taras Buria Apple has unveiled watchOS 26, another operating system under the new naming scheme (all of Apple's systems now feature the same version number to match the current/upcoming year) and "Liquid Glass" Redesign. The latter is a dynamic interface that features translucency, reflections, parallax, and other visual candy. Aside from the redesign and a new number, Apple is introducing new health and workout features. Workout Buddy is a feature that offers personalized, spoken motivation during workouts. It analyzes your pace, heart rate, and fitness history, delivering real-time encouragement and more engagement, like having someone else work out next to you, providing useful information and cheering you up. Here is an example of what you will hear with Workout Buddy: The Workout app also has a more streamlined interface with four buttons in the corners for quick access to favorite features during workouts. You can set up music and podcasts right in the Workout app so that your favorite content starts playing when you begin your workout. In addition, Apple announced a new one-handed wrist flick gesture that allows users to dismiss notifications with a quick flick, which is convenient when you cannot use your both hands to interact with your watch. Other changes include the Notes app, which now lets you access notes on the Apple Watch, Hold Assist and Call Screening in the Phone app, Live Listen with real-time Live Captions of what the iPhone hears, improvements to the Photos face, and more. watchOS 26 will be available later this year, and the first developer betas are expected soon.
    • tvOS 26 arrives with karaoke mode, updated profile switcher, Liquid Glass, and more by Aditya Tiwari WWDC 2025 is now live, and Apple has finally renamed all of its operating systems to version 26. This year, we have tvOS 26 for the Apple TV, featuring the freshly-baked Liquid Glass software design. Apple explained that Liquid Glass brings a fresh and vibrant look to Apple TV, "delivering a fresh and expressive design that beautifully reflects and refracts its surroundings using real-time rendering." Content is the primary focus of the experience when you fast-forward, rewind, or change a setting from the Control Center. The redesigned Apple TV app now features new poster art for movies and TV shows, with a touch of Liquid Glass. Users can now choose whether to display the profile switcher UI when their Apple TV wakes up, allowing them to quickly access their Watchlist and recommendations in the TV app, as well as playlists in Apple Music. Speaking of Apple Music, your iPhone can now double as a handheld microphone for Apple TV. The new karaoke mode allows multiple users to queue up songs from their iPhones and sing along, with support for lyrics translation and pronunciation. Apple has updated the FaceTime experience to make it consistent with iPhone. Contact Posters introduced with iOS 17 are now available on Apple TV, which display a contact's customized photo and name when starting a FaceTime call. Live Captions support on FaceTime now includes French, German, Japanese, Korean, Mandarin, and Spanish. FaceTime on Apple TV has been updated to display phone and voice call notifications for active profiles. Users can use their connected HomePod speakers or iPhone to answer them. Apart from that, tvOS 26 also brings new Aerial screen savers, featuring India's Goa and Kerala. Users can also personalize their screensaver experience and designate any AirPlay-enabled speaker as the permanent speaker for Apple TV. tvOS 26 has also made the sign-in experience a bit easier for users. Developers can link app logins to an Apple account, making it quicker for users to access their apps and content when setting up a new device. The new tvOS update is now available for testing through the Apple Developer Program, supported on Apple TV 4K (2nd generation and later). Meanwhile, the updated sing-along feature is available on iPhone 11 and later with Apple TV 4K (3rd generation).
  • Popular Contributors

    1. 1
      +primortal
      479
    2. 2
      +FloatingFatMan
      276
    3. 3
      ATLien_0
      257
    4. 4
      Edouard
      205
    5. 5
      snowy owl
      200
  • Tell a friend

    Love Neowin? Tell a friend!