CSOM returns 403 Forbidden when checking site collection
-
04-10-2020 - |
سؤال
I am creating a console application to create Subsite's, lists, list items etc. I have a simple function that checks if a SiteCollection
exists and am met with a 403 Forbidden
error when calling the ClientContext.ExecuteQuery()
function.
Example Code:
using (ClientContext ctx = new ClientContext(url))
{
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery(); //403 Error
}
So I read this thread which detailed passing NetworkCredential
object to the context. So I changed my code to:
using (ClientContext ctx = new ClientContext(url))
{
Web web = ctx.Web;
ctx.Load(web);
ctx.Credentials = new NetworkCredential("MyUserName", "pass", "domain");
ctx.ExecuteQuery(); //Still 403's
}
It still 403's and I have gone back to verify the accounts credentials and permissions across the site that I am attempting to access.
Is there an issue with my code or is it something else? Can CSOM
go cross server?
UPDATE
The code above works on a server with Active Directory
, but not a server utilizing ADFS
. ADFS
possibly blocking this call?
المحلول 2
Just want to add this here. Unfortunately the suggested answers of adding NetworkCredential
did not work. I instead switched from using Microsoft.SharePoint.Client
dll to the Microsoft.SharePoint
library. Everything works now except with the caveat of course that my application now has to be run on a SharePoint server.
نصائح أخرى
Try setting ctx.Credentials
before.
Other than that, double check your user/pwd/domain
using (ClientContext ctx = new ClientContext(url))
{
ctx.Credentials = new NetworkCredential("MyUserName", "pass", "domain");
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery(); //Still 403's
}
After struggling with the same issue, I looked at the SharePoint Office PNP core samples, and tried to login using the default browser login, you can try the below sample I got working
string myAdfsSharePoint= "https://abc.myTenant.com/sites/abc";
OfficeDevPnP.Core.AuthenticationManager am = new OfficeDevPnP.Core.AuthenticationManager();
SP.ClientContext ctx = am.GetWebLoginClientContext(myAdfsSharePoint);
Also, make sure to have the default browser signed in, Since I had to prepare an Offline utility for my Client, I was allowed to ! I had to include this DLL from Github OfficeCorePnp Samples