Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:41:10+00:00 2026-06-02T13:41:10+00:00

I am having to re-write an existing REST API using .NET (originally written with

  • 0

I am having to re-write an existing REST API using .NET (originally written with Ruby). From the client’s perspective, it has to work exactly the same way as the old API – i.e. the client code mustn’t need to change. The current API requires Basic Authentication. So to call the old API, the following works perfectly:-

        var wc = new System.Net.WebClient();
        var myCache = new CredentialCache();
        myCache.Add(new Uri(url), "Basic", new NetworkCredential("XXX", "XXX"));
        wc.Credentials = myCache;
        var returnBytes = wc.DownloadData("http://xxxx");

(I have had to ommit the real URL / username / password etc for security reasons).

Now I am writing the new API using ASP.Net Web API with MVC4. I have a weird problem and cannot find anybody else with exactly the same problem. In order to support Basic Authentication, I have followed the guidelines here:

http://sixgun.wordpress.com/2012/02/29/asp-net-web-api-basic-authentication/

One thing, I put the code to “hook in the handler” in the Global.asax.cs file in the Application_Start() event (that wasn’t explained so I guessed).

Anyway, if I call my API (which I have deployed in IIS) using the above code, the Authorization header is always null, and the above fails with 401 Unauthorized. However, if I manually set the header using this code, it works fine – i.e. the Authorization header now exists and I am able to Authenticate the user.

    private void SetBasicAuthHeader(WebClient request, String userName, String userPassword)
    {
        string authInfo = userName + ":" + userPassword;
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        request.Headers["Authorization"] = "Basic " + authInfo;
    }
   .......
    var wc = new System.Net.WebClient();
    SetBasicAuthHeader(request, "XXXX", "XXXX");
    var returnBytes = wc.DownloadData("http://xxxx");

Although that works, it’s no good to me because existing users of the existing API are not going to be manually setting the header.

Reading up on how Basic Authentication works, the initial request is meant to be anonymous, then the client is returned 401, then the client is meant to try again. However if I put a break point in my code, it will never hit the code again in Antony’s example. I was expecting my breakpoint to be hit twice.

Any ideas how I can get this to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:41:13+00:00Added an answer on June 2, 2026 at 1:41 pm

    You’re expecting the right behavior. System.Net.WebClient does not automatically include the Authorization headers upon initial request. It only sends them when properly challenged by a response, which to my knowledge is a 401 status code and a proper WWW-Authenticate header. See here and here for further info.

    I’m assuming your basic authentication handler is not returning the WWW-Authenticate header and as such WebClient never even attempts to send the credentials on a second request. You should be able to watch this in Fiddler or a similar tool.

    If your handler did something like this, you should witness the WebClient approach working:

    //if is not authenticated or Authorization header is null
    return base.SendAsync(request, cancellationToken).ContinueWith(task =>
        {
            var response = task.Result;
            response.StatusCode = HttpStatusCode.Unauthorized;
            response.Headers.Add("WWW-Authenticate", "Basic realm=\"www.whatever.com\"");
            return response;
        });
    
    //else (is authenticated)
    return base.SendAsync(request, cancellationToken);
    

    As you noticed, if you include the Authorization headers on every request (like you did in your alternative approach) then your handler already works as is. So it may be sufficient – it just isn’t for WebClient and other clients that operate in the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having to write a TCP Server for a project at work and I
I am having trouble figuring out how to write to a file using fopen
I am having a problem with my code. I am using VB.NET and Visual
I've set up a REST service and client in PHP and I'm having a
I am trying to use boost lambda to avoid having to write trivial functors.
CommandBuilder is a very handy class that helps us avoid having to write INSERT,UPDATE
For this question, we want to avoid having to write a special query since
I am attempting to write a bash script and I am having difficulty making
I'm having trouble wrapping my head around how to write this query. A hypothetical
Having this method: public Boolean isCorrect() { return correct; } I can't write: @XmlType(propOrder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.