The remote server returned an error (401) Unauthorized SharePoint Online/2016/2013 CSOM or PowerShell

In this SharePoint tutorial, we will discuss how to resolve The remote server returned an error (401) Unauthorized error in SharePoint Online/2016/2013 with CSOM or PowerShell. The remote server returned an error (401) Unauthorized error can come in SharePoint Online as well as 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 which comes while working with SharePoint 2016/2013 client object model (csom/c#). The error comes are 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 SharePoint 2016/2013 .Net client object model to retrieve site information using visual studio 2015/2017.

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

This is a simple code we were writing to retrieve 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 change the credentials the issue did not came.

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

Here basically we need to check 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 SharePoint provider hosted app

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

If after giving correct credentials, still, the error 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 below solution also.

Open web.config file of 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 were connecting to SharePoint online site using SharePoint online management shell.

Below was 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 replace the URL like https://onlysharepoint2013-admin.sharepoint.com/

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

You may like following SharePoint csom tutorials:

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.

>