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?
 
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
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.