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.
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(@"SP2016Bijay", "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:UsersAdministratordocumentsvisual studio 2015ProjectsClientObjectModelDemo17MarchClientObjectModelDemo17MarchForm1.cs:line 99
at ClientObjectModelDemo17March.Form1.button2_Click(Object sender, EventArgs e) in E:UsersAdministratordocumentsvisual studio 2015ProjectsClientObjectModelDemo17MarchClientObjectModelDemo17MarchForm1.cs:line
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(@"SP2016Bijay", "mypassword");
When I change the credentials the issue did not came.
context.Credentials = new NetworkCredential(@"MySPAdministrator", "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(@"TSInfoAdministrator", "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
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:
- Create subsite in SharePoint 2016 programmatically using csom visual studio 2017
- the remote server returned an error 500 internal server error. the sharepoint client object model
- the remote server returned an error (403) forbidden SharePoint client object model
- Get list item by id using jsom (JavaScript object model) in SharePoint Online/2016/2013
- Get all SharePoint subsites programmatically using PnP CSOM
- Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server error in PowerShell SharePoint Online
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.
Hello Everyone!! I am Bhawana a SharePoint MVP and having about 10+ years of SharePoint experience as well as in .Net technologies. I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools. Now exploring more in SharePoint 2016 🙂 Hope here I can contribute and share my knowledge to the fullest. As I believe “There is no wealth like knowledge and no poverty like ignorance”
- Remove From My Forums
-
Question
-
Hi,
Recently I was attempting to brand a clients SharePoint 2013 site. After I deployed the solution, set it to custom/default master page and suddenly I get «Unauthorized 401» all over the place with URL «start.aspx». I looked around and
found out that this only happened when having the «Minimal Download Strategy» site feature enabled and SharePoint run the request through the start.aspx page.After I disabled the MDS Feature, then I am able to open site home page, document library again. I don’t know what is the the real reason for such error.
MS best practice it to disable the MDS Featue? Or any suggestion? What is the root cause for such error?
Hope someone here can assist me.
-
Edited by
Wednesday, May 18, 2016 8:25 AM
-
Edited by
Answers
-
-
Proposed as answer by
Wendy DZMicrosoft contingent staff
Saturday, May 21, 2016 12:34 AM -
Marked as answer by
Wendy DZMicrosoft contingent staff
Sunday, May 29, 2016 1:47 PM
-
Proposed as answer by
The
remote server returned an error 401 unauthorized
<%@ Page Language="C#" contentType="BinaryStream" %>
<
script
language
=
"c#"
runat
=
"server"
>
protected void Page_Load(object sender, EventArgs e)
{
string strURL = Microsoft.SharePoint.SPContext.Current.Web.Url + "/_vti_bin/owssvr.dll?Cmd=Display&List=161D7884-FD59-4464-AF00-40F653A214F8&XMLDATA=TRUE";
System.Net.HttpWebRequest objWebRequest = null;
System.Net.HttpWebResponse objWebResponse = null;
System.IO.StreamReader streamReader = null;
string strHTML = null;
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate
certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };
objWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
objWebRequest.Method = "GET";
objWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
objWebResponse = (System.Net.HttpWebResponse)objWebRequest.GetResponse();
streamReader = new System.IO.StreamReader(objWebResponse.GetResponseStream());
strHTML = streamReader.ReadToEnd();
streamReader.Close();
objWebResponse.Close();
objWebRequest.Abort();
Response.ContentType = "text/xml";
Response.Write(strHTML);
Response.End();
}
</
script
>
Actions Required Resolve the «error 401 unauthorized»
YOU COULD REFER TO KB896861 (HTTP://SUPPORT.MICROSOFT.COM/KB/896861)
TO WORKAROUND THE ISSUE.
I QUOTED THE METHOD 2 IN THE KB FOR YOUR INFORMATION:
TO DISABLE THE LOOPBACK CHECK. FOLLOW THESE STEPS:
- CLICK START, CLICK RUN, TYPE
REGEDIT, AND THEN CLICK OK. - IN REGISTRY EDITOR, LOCATE AND THEN CLICK THE FOLLOWING REGISTRY KEY:
HKEY_LOCAL_MACHINESYSTEMCURRENTCONTROLSETCONTROLLSA - RIGHT-CLICK
LSA, POINT TO NEW, AND THEN CLICK DWORD VALUE.
- TYPE DISABLELOOPBACKCHECK, AND THEN PRESS ENTER.
- RIGHT-CLICK DISABLELOOPBACKCHECK, AND THEN CLICK
MODIFY. - IN THE VALUE DATA BOX, TYPE
1, AND THEN CLICK OK. - QUIT REGISTRY EDITOR, AND THEN RESTART YOUR SERVER.
References
The following reference is related to my issue.
- http://social.msdn.microsoft.com/Forums/en/sharepointinfopath/thread/544a81dc-214a-4caf-aa42-fc9dec796882
i have my sharepoint setup, and am rolling it out to the users.
on some PCs, i’ve experienced «401 Unauthorized» when the user is trying to connect.
ie
I have full access, and have granted access to a few users (read).
one user, it logged in straight away, didn’t prompt for username or password.
then, for another user, he was prompted for his username and password. but it gave him the 401.
Also, i went logged on to another PC, and it gave me the 401.
Could set up of browsers be causing the issue?
Any suggestions ?
thanks
We may run into an exception when you call the OoB list web services from the Infopath Web-based SharePoint 2010. This exception is “The remote server returned an error: (401) Unauthorized. The remote server returned an error: (401) Unauthorized.“
NetScaler balances the load in front of the multiple IIS servers and the VIP setup is for the Webapps. However, if the web services which are invoked from each individual services such as from the Infopath Web-based form, it generates an exception. You may have this issue.
Then there are the possibilities that can lead to (401) Unauthorized error. The two major issues may arise when you are using a load balancer with the multiple IIS servers and set up the alias or VIP for the web app are authentication loopback issues and the load balancer did not return the localhost IP issue. You need to fix both of these in order to make the web services calls successful. Here are the methods to fix them.
Solution 1 – Authentication Loopback Issue
The cause behind this issue is 3.5 sp1- to fix this you need to add a key to the registry to disable authentication double lookup. To do this you need to set the DisableLoopbackCheck registry entry in the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlLsa registry subkey to 1.
For setting the DisableLoopbackCheck registry entry to 1, you need to follow these steps on the client computer.
- Firstly you need to click the Start, click Run, type regedit, and then click OK.
- This will Locate and then click the following registry subkey: HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlLsa
- Then you need to Right-click on Lsa, point to New, and then click DWORD Value.
- Then you need to Type DisableLoopbackCheck and then press ENTER.
- Then Right-click DisableLoopbackCheck, and then click Modify.
- In the Value data box, type 1, and then click OK.
- Then Exit Registry Editor.
- Restart the computer
Here is the Microsoft link that you can go through to get the details.
Solution 2 – Load balancer web service call does not return back to the local server
It seems that the web services are like the web service call fails that calls the call initialized from one of the servers behind the Load balancer that points the web services that use the VIP alias.
Here are the steps that help to find the IPs and fix it.
Firstly you need to identify the IP for Webapp VIP alias. If your site URL for this http://SharePointWebApp, from where you can identify the IP for it from the IIS server by using the command and the IP which is 22.44.555.66.
C: >ping SharePointWebApp
The pinging sharepoinwebapp.compony.com [22.44.555.66] with 32 bytes of data:
Reply from 22.44.555.66: bytes=32 time=1ms TTL=255
Secondly, then you need to find the IP for the IIS server behind the load balancer that uses the command ipconfig such as the IP is 77.33.99.44.
C:Usersosstst1>ipconfig /all
Windows IP Configuration
Host Name . . . . . . . . . . . . : xxxxxxxxx
Primary Dns Suffix . . . . . . . : na.compony.com
Node Type . . . . . . . . . . . . : Peer-Peer
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : compony.com
……
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 77.33.99.44(Preferred)
Thirdly you need to add the one line entry into the host file that is located at the
C:WindowsSystem32driversetchost. Add the additional line:
77.33.99.44 SharePointWebApp
Fourthly, you need to flush the DNS Resolver Cache that uses the command and verifies the WebApp VIP IP original value 22.44.555.66 that returns the ping that will be changed to the local IIS server that is 77.33.99.44.
C:>ipconfig /flushdns
C: >ping SharePointWebApp
The Pinging sharepoinwebapp.compony.com [77.33.99.44] with 32 bytes of data:
Reply from 22.44.555.66: bytes=32 time=1ms TTL=255
Now the web service will be called by the Infopath Webbased form on the SharePointIIS server that will not go back to the load balancer. It will call the web services that are hosted by the same host server. By changing the two settings you should be able to resolve (401) unauthorized error.
Best & Easy Solutions to Fix SharePoint 401 Unauthorized Error
If this solution does not solve your problem then you need not worry when you have SharePoint Repair Tool, it is a powerful tool that effectively scans the damaged database files and repairs it to recover inaccessible objects in MDF database files.
This is an advanced tool that helps you too safely repairs SharePoint databases or SQL Server files. It just by scanning once detect and fix the SQL server errors. It not only repairs the files but also recovers entire crucial information stored on your SharePoint Server.
It even allows the users to retrieve the share point database like tables, indexes, documents, labels, and stored procedures from inaccessible MDF files. This software is incorporated with a simple and rich graphical user interface to offer easy SharePoint recovery.
Steps to Run the SharePoint Repair Tool
- Firstly, download, install, and launch the SharePoint Recovery Tool.
- The interface screen displays two options to recover SharePoint data: ‘Complete Repair‘ and ‘Document Recovery‘. Select as per your requirement.
- If you choose ‘Complete Repair‘, the screen will show the options for selecting and searching SQL Server (.MDF) files. Click on ‘Browse‘ to select a SharePoint database for repair.
- If you do not know the actual path of the database which you want to repair, click on the ‘Find‘ to locate the MDF file in a specific drive.
- After selecting the desired file initiate the scanning process.
- After finishing the scan, the software would generate a tree of all SharePoint database tables and will display it in the left pane. You can select the table in the tree and can have the entire preview in the right pane. Then click on ‘Repair‘ to begin repairing for the selected database.
- After clicking on ‘Repair‘, this dialog box will appear. Specify the SQL Server name or Instance name and the desired destination path.
- Click on the ‘Browse‘ option to choose the destination path. And select the ‘OK‘ button.
- Once this process gets completed, you would find a dialog box that displays this message ‘Recovered file saved at the desired location.” This shows that Recovery Process has successfully completed’.
- Select the ‘OK‘ button.
- You would find another dialog box that asks whether you want to attach the repaired database to the web application. If you click on the ‘Yes‘ button, the above dialog box will be displayed.
- In the web application type URL, the name of the corrupt database and SQL Server Instance name.
- Then select the ‘OK‘ button and proceed.
- When the process gets finished, you would be able to access the repaired database by opening the web application.
Conclusion
So, this is all about the SharePoint 2013 401 unauthorized error webservice, I tried my best to list down the best possible fixes to get rid of the 401 unauthorized SharePoint error.
Try the solutions given one by one carefully and fix SharePoint 2013 error 401 unauthorized.
Hope the solutions given works for you. But if not then feel free to use the professional recommended SharePoint Repair Tool.
If you have any other queries or suggestions, then share it with us on Facebook and Twitter. Also, if you like this article, then share it with the users going through the same issue.
Jacob Martin is a technology enthusiast having experience of more than 4 years with great interest in database administration. He is expertise in related subjects like SQL database, Access, Oracle & others. Jacob has Master of Science (M.S) degree from the University of Dallas. He loves to write and provide solutions to people on database repair. Apart from this, he also loves to visit different countries in free time.