The remote server returned an error (401) Unauthorized in SharePoint

In this SharePoint tutorial, we will discuss how to fix the error “The remote server returned an error (401) Unauthorized” in SharePoint. The remote server returned an error (401) Unauthorized error can come in SharePoint Online, SharePoint 2019, and SharePoint 2016/2013.

I have faced the (401) Unauthorized error while working with csom in both SharePoint Online and SharePoint 2016 and with PowerShell.

The remote server returned an error: (401) Unauthorized SharePoint Online csom

First, we will discuss how to resolve the issue that comes up while working with the SharePoint client object model (csom/c#). The error comes as An unhandled exception of type ‘System.Net.WebException’ occurred in Microsoft.SharePoint.Client.dll. Additional information: The remote server returned an error: (401) Unauthorized.

We were using the SharePoint .Net client object model to retrieve site information using Visual Studio.

We were using Microsoft.SharePoint.Client.dll & Microsoft.SharePoint.Client.Runtime.dll. We were writing the below code inside a console application for SharePoint.

We were writing This simple code to retrieve the web title from the SharePoint 2016 site collection.

private void GetSiteDetails()
{
using (ClientContext context = new ClientContext("http://mypc/sites/MySP2016SiteCollection/"))
{
context.Credentials = new NetworkCredential(@"SP2016\Bijay", "mypassword");
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
lblResult.Text = web.Title;
}
}

But in the ExecuteQuery() method it gave the error like below:

An unhandled exception of type ‘System.Net.WebException’ occurred in Microsoft.SharePoint.Client.dll

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

at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at ClientObjectModelDemo17March.Form1.GetSiteDetails() in E:\Users\Administrator\documents\visual studio 2015\Projects\ClientObjectModelDemo17March\ClientObjectModelDemo17March\Form1.cs:line 99
at ClientObjectModelDemo17March.Form1.button2_Click(Object sender, EventArgs e) in E:\Users\Administrator\documents\visual studio 2015\Projects\ClientObjectModelDemo17March\ClientObjectModelDemo17March\Form1.cs:line

the remote server returned an error (401) unauthorized sharepoint csom
the remote server returned an error (401) unauthorized sharepoint csom

The remote server returned an error: (401) Unauthorized SharePoint CSOM

We need to provide credentials when we are trying to connect to SharePoint on-premise or SharePoint online site. In my case, I was not providing the credentials correctly. So the above error was coming.

The issue was in the below line:

context.Credentials = new NetworkCredential(@"SP2016\Bijay", "mypassword");

When I changed the credentials, the issue did not come.

context.Credentials = new NetworkCredential(@"MySP\Administrator", "Qwe@12345");

Here, basically, we need to check whether the credentials are correct or not. If you provide the correct credentials, then it will work fine.

the remote server returned an error 401 unauthorized in SharePoint provider hosted app

The same error “the remote server returned an error 401 unauthorized,” can also appear in SharePoint Provider-hosted apps or add-ins.

If, after giving the correct credentials, the error still exists, then you can try to set AuthenticationMode to Default like below:

using (ClientContext context = new ClientContext("http://tsinfotech/sites/TSINFOTECH/"))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = new NetworkCredential(@"TSInfo\Administrator", "Welcome@123");

//Rest of your code

}

If you are facing the same issue in Provider hosted apps, then you can try the solution below.

Open the web.config file of the asp.net web form application and write the below code.

<appSettings>
   <add key="aspnet:AllowAnonymousImpersonation" value="false" />
</appSettings>

The remote server returned an error (401) Unauthorized SharePoint PowerShell

Recently, while working with PowerShell SharePoint using SharePoint online management shell, we got the below error, which says:

Connect-SPOService : The remote server returned an error: (401) Unauthorized.
At line:2 char:1
Connect-SPOService -Url http://onlysharepoint2013-admin.sharepoint.com/ -credent …
CategoryInfo : NotSpecified: (:) [Connect-SPOService], WebException
FullyQualifiedErrorId : System.Net.WebException,Microsoft.Online.SharePoint.PowerShell.ConnectSPOService

Get-SPOSite : No connection available. Use Connect-SPOService before running this CmdLet.
At line:3 char:1
Get-SPOSite
CategoryInfo : NotSpecified: (:) [Get-SPOSite], InvalidOperationException
FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Online.SharePoint.PowerShell.GetSite

We connected to the SharePoint online site using the SharePoint online management shell.

Below is the command we were trying.

Import-Module Microsoft.Online.SharePoint.Powershell -Verbose
Connect-SPOService -Url http://onlysharepoint2013-admin.sharepoint.com/ -credential Bhawana@OnlySharePoint2013.onmicrosoft.com
Get-SPOSite

the remote server returned an error (401) unauthorized sharepoint powershell
the remote server returned an error (401) unauthorized sharepoint Powershell

We were giving the -admin site also in the Url parameter, but still, the issue was coming.

the remote server returned an error (401) unauthorized SharePoint PowerShell

We found out that the URL was creating the issue. we were providing http://onlysharepoint2013-admin.sharepoint.com/ (with HTTP, not with https)

We need to provide the URL as https URL. Office 365 URLs are always https.

So we replaced the URL with https://onlysharepoint2013-admin.sharepoint.com/

This resolved the issue. We did not receive any. The remote server returned an error: (401) Unauthorized error.

The same solution applies to the below error messages which are related:
the remote server returned an error (401) unauthorized. SharePoint online,
connect-pnponline : the remote server returned an error: (401) unauthorized., the remote server returned an error (401) unauthorized SharePoint PowerShell, the remote server returned an error 401 unauthorized, the remote server returned an error 401 unauthorized SharePoint, the remote server returned an error 401 unauthorized SharePoint csom, the remote server returned an error 401 unauthorized SharePoint provider hosted app, the remote server returned an error (401) unauthorized office 365, the remote server returned an error (401) unauthorized SharePoint 2013/2016.

You may also like:

>