SharePoint 2019 On Premise: System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.'

SHUO .H 706 Reputation points

Hi there,

When I use CSOM to connect the SharePoint Site (2* WFE and AAM is, and the following error comes up as shown below:

System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.'

my code is:

ClientContext clientContext = new ClientContext(siteUrl);

            clientContext.Credentials = new NetworkCredential(farmAdmin, password, domain);
            clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

            Uri uri = new Uri(siteUrl);

            if (uri.Scheme == "https")
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);


            return clientContext;

even I use the PnP component (OfficeDevPnP.Core: OfficeDevPnP.Core.AuthenticationManager authenticationManager) is the same error.

is there any solution on it.? Thanks

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
1,811 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,439 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jerryzy 10,421 Reputation points

    Hi @SHUO .H ,

    I understand the 401 error is coming when access site remotely with CSOM.

    Here are some suggestions to narrow down the issue:

    1. Try to create a new web application without any AAM/Certificate settings and test if CSOM can connect to the site host in the new web application. If the CSOM is working, then the issue is related to the AAM binding, need to check in IIS and web.config.
    2. If the issue is the same for the new web application, then it needs to check the CSOM request with Fiddler and check ULS log for detailed information.

    Best Regards

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.