The remote server returned an error: (401) Unauthorized.

Hi, I got "The remote server returned an error: (401) Unauthorized. " error while I try to call web service using webclient with Network Credential Account. IIS is using Anonymous Authentication. I doubt that  Network Credential will not work properly with Anonymous Authentication. Kindly help me to solve the issue, thanks in advance.

 Below is my code:

       Uri downloadURI = new Uri(HttpUtility.UrlDecode(strURL));
        WebClient webClientObj = new WebClient();    
        NetworkCredential nw = new NetworkCredential(ServiceUser, ServicePassword,ServiceDomain);
        webClientObj.UseDefaultCredentials = false;
        webClientObj.Credentials = nw;

        isExist = false;
        string fileName = tempPath + @"\" + downloadURI.Segments[downloadURI.Segments.Length - 1].Replace("%20", "_");
        try
        {
            if (!File.Exists(fileName))
            {
                webClientObj.DownloadFile(downloadURI, fileName);
            }
            else
            {
                isExist = true;
            }
        }
        catch (Exception e)
        {
             fileName = string.Empty;
             throw e;
        }
        finally
        {
            webClientObj.Dispose();
            webClientObj = null;
        }
LVL 2
hollowkidoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
Authorization I doubt that  Network Credential will not work properly with Anonymous Authentication
There is authentication and authorization. you are not doing any authentication therefore you have to have in your authorization all users or anonymous users
0
hollowkidoAuthor Commented:
Hi David, Thanks for your advice, kindly see my below IIS authorization set up IISauth.png. but I still got the 404 unauthorized error.
0
hollowkidoAuthor Commented:
Here is my situation, we are migrating one of the SP 2007 site to SP2010 server.
Inside that SP Site, there is one custom web part calling a webservice to download the files from sharepoint to Server location. Most of the people said this could be NTLM double hoop Issue while IIS passing the credential to Sharepoint DB server. But to overcome this situation, I have already pass the Network Credential while downloading the file. We could used this piece of code in our UAT environment. ( we have one Front End Server, one App Server and one DB server for UAT). This error occur in our Production environment which we have two front end servers , two app servers and one DB server). Really appreciate for any help.
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

David Johnson, CD, MVPOwnerCommented:
a 404 error is a file not found error

401 Unauthorized
    Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
0
hollowkidoAuthor Commented:
Hi Dave, it is my typo error, the error is 401 unauthorized.
0
Bob LearnedCommented:
What is the authentication type in the web.config?   Are you using Forms Authentication?
0
hollowkidoAuthor Commented:
Hi Bob, as I said earlier I am using Anonymous Authentication. And yesterday we found out that if we didn't used load balancing for our two web front end servers and just point directly to one of the servers, the web service is working properly. So, I would like to know this issue is related to Load balancer? If so, if you guys have any ideas on this load balancing and webservice calling from sharepoint custom web part? Thank you.
0
Bob LearnedCommented:
With anonymous, it should allow access to everyone, so it makes sense that the web service is not the problem.  It really depends on the load balancer, but I believe that most (if not all) have auth pass through:

Oracle GlassFish Server 3.1-3.1.1 High Availability Administration Guide
http://docs.oracle.com/cd/E18930_01/html/821-2426/abdhs.html

The auth-pass-through-enabled Attribute
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.