locked
SharePoint .net CSOM getting "The remote server returned an error: (403) Forbidden" RRS feed

  • Question

  • Hi,

    We are getting "The remote server returned an error: (403) Forbidden"  suddenly in production environment while using .Net CSOM. We tried to read the production site title with the below code, still facing same issue.

    static void Main(string[] args)

            {

              

                ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

                ClientContext clientContext = new ClientContext(@"https://sitename...");

                Web site = clientContext.Web;

                clientContext.Load(site);

                clientContext.ExecuteQuery();

                Console.WriteLine("Title: {0}", site.Title);

                Console.ReadLine();

    The Exception- The remote server returned an error: (403) Forbidden.

    We have also confirmed the user is site collection admin under which this above code is running.

    After analyzing the response in the debug mode we get the following details

    {SPRequestGuid: bac0759b-03c9-4b3f-85e1-93ee7a6505f4

    X-SharePointHealthScore: 0

    X-Forms_Based_Auth_Required: https://sitename_login/SPORedirectMultiAuthLogin.aspx?ReturnUrl=/_layouts/Error.aspx&Source=%2fsites%2fAAAAA4882%2fAPEX%2f_vti_bin%2fsites.asmx

    X-Forms_Based_Auth_Return_Url: https://sitename/_layouts/Error.aspx

    X-MSDAVEXT_Error: 917656; Access denied. Before opening files in this location, you must first browse to the web site and select the option to login automatically.

    MicrosoftSharePointTeamServices: 14.0.0.7106

    X-Frame-Options: SAMEORIGIN

    Content-Length: 13

    Cache-Control: private, max-age=0

    Date: Tue, 11 Feb 2014 04:53:25 GMT

    Set-Cookie: spodsession=384799110.47873.0000; path=/

    Server: Microsoft-IIS/7.5

    X-AspNet-Version: 2.0.50727

    X-Powered-By: ASP.NET

    }

    Regards,

    Sidd

    Tuesday, February 11, 2014 11:54 AM

Answers

  • Hi,

    The basic problem that I see here is authentication or authorization.

    If you are using Claims authentication, please try the following code by Steve Peschka.

     ClientContext ctx = new ClientContext(SamlTxt.Text);

                    //use default credentials
                    ctx.Credentials = CredentialCache.DefaultCredentials;

                    //configure the handler that will pick up the auth cookie
                    ctx.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(ctx_ExecutingWebRequest);

                    //get the web
                    Web w = ctx.Web;

                    //LOAD LISTS WITH ALL PROPERTIES
                    var lists = ctx.LoadQuery(w.Lists);

                    //execute the query
                    ctx.ExecuteQuery();

    The whole code is available for download here ...

    http://blogs.technet.com/cfs-filesystemfile.ashx/__key/telligent-evolution-components-attachments/01-7508-00-00-03-33-59-40/ClientOmAuth.zip


    Please mark the replies as answers if they help or unmark if not.

    • Marked as answer by SiddharthaDas Thursday, February 13, 2014 6:57 AM
    Wednesday, February 12, 2014 3:09 AM

All replies

  • Hi,

    According to your post, an error occurred when you used Client Object Model to connect SharePoint 2010.

    Please do as follows:

    1. Try to disable the Active Directory Federation Services(ADFS).
    Here is a similar thread for you to take a look at:
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/ab35812d-da5c-413f-9323-659944e9b0c7/the-remote-server-returned-an-error-403-forbidden-on-executequery?forum=sharepointdevelopmentprevious

    2. Try to add credentials to your clientContext object.

    NetworkCredential credentials =new NetworkCredential("username", "pwd", "domain");
    clientContext .Credentials = credentials;

    or use

    clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    Best Regards


    Dennis Guo
    TechNet Community Support

    Wednesday, February 12, 2014 2:05 AM
  • Hi,

    The basic problem that I see here is authentication or authorization.

    If you are using Claims authentication, please try the following code by Steve Peschka.

     ClientContext ctx = new ClientContext(SamlTxt.Text);

                    //use default credentials
                    ctx.Credentials = CredentialCache.DefaultCredentials;

                    //configure the handler that will pick up the auth cookie
                    ctx.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(ctx_ExecutingWebRequest);

                    //get the web
                    Web w = ctx.Web;

                    //LOAD LISTS WITH ALL PROPERTIES
                    var lists = ctx.LoadQuery(w.Lists);

                    //execute the query
                    ctx.ExecuteQuery();

    The whole code is available for download here ...

    http://blogs.technet.com/cfs-filesystemfile.ashx/__key/telligent-evolution-components-attachments/01-7508-00-00-03-33-59-40/ClientOmAuth.zip


    Please mark the replies as answers if they help or unmark if not.

    • Marked as answer by SiddharthaDas Thursday, February 13, 2014 6:57 AM
    Wednesday, February 12, 2014 3:09 AM
  • Hi Sundar,

    Thanks for pointing me towards right direction.

    Now what I can see if I use  "X-FORMS_BASED_AUTH_ACCEPTED", "f"  I am able to fetch data from the site. What I understand is claims authentication is not working as expected on this site(though the site is configured for claim), therefore, if I use windows authentication( "X-FORMS_BASED_AUTH_ACCEPTED", "f") then I am able to access the site.

    Could u please tell me what has to be done on the security level for the site, so that site is accessible without using ( "X-FORMS_BASED_AUTH_ACCEPTED", "f") 

    Thursday, February 13, 2014 6:56 AM
  • Hi Sundar,

    I am getting this error when I am writing the below code for SharePoint online in console application. Any idea what is the problem?

    using (ClientContext clientContext = new ClientContext("SharepointOnlineSite")) { string userName = "xyz"; string password = "abc"; SecureString secpassword = new SecureString(); foreach (char c in password.ToCharArray()) secpassword.AppendChar(c); // SharePoint Online Credentials clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest; clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; // Get the SharePoint web Web web = clientContext.Web; // Load the Web properties clientContext.Load(web); // Execute the query to the server. clientContext.ExecuteQuery(); // Web properties - Display the Title and URL for the web Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url); Console.ReadLine(); Console.ReadKey();

    }

    Regards

    Agasthya


    agasthya


    • Edited by Agasthya.S.P Thursday, September 6, 2018 1:07 PM update
    Thursday, September 6, 2018 1:05 PM
  • Recently I was trying to retrieve the site title using csom in a console application, I got the same error. Thought of posting, so that it will <g class="gr_ gr_199 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="199" id="199">helpful</g> to users facing the same issue.

    the remote server returned an error (403) forbidden SharePoint client object model


    Many Thanks,
    Bijay Kumar
    EnjoySharePoint.com | SharePointSky.com | TSInfoTechnologies.com

    Bijay Kumar MVP

    Tuesday, March 26, 2019 7:12 AM