Remote Authentication in SharePoint Online Using the Client Object Model

This sample demonstrates how to authenticate against Microsoft SharePoint Online in client applications using the managed SharePoint client-side object models.

C# (51.6 KB)
 
 
 
 
 
3.4 Star
(28)
25,140 times
Add to favorites
4/28/2011
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • how can i read file using this auth way
    1 Posts | Last post January 05, 2018
    • i was trying to use 
      FileInformation fileInformation = File.OpenBinaryDirect(cxt, (string)f.ServerRelativeUrl);
                                  using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
                                  {
                                      // Read the stream to a string, and write the string to the console.
                                      String line = sr.ReadToEnd();
                                      Console.WriteLine(line);
                                  }
      
      it gives me 403 forbiden
      
      but i can use other methods to retrieve file/folder list, but not the context
      
      please help
      
      Thanks,
      
      FYI , i am using sharepoint online
  • Error: The requested site does not appear to have claims enabled or the Login Url has not been set
    1 Posts | Last post May 24, 2017
    • Does anybody have the solution to this error? I managed to do it successfully in my local workstation. However after i published and deploy the aspx on my Windows Server 2008, i get the error when accessing the page. 
      I will really appreciate any help as I'm stuck at this error. 
  • The remote server returned an error: (401) Unauthorized
    2 Posts | Last post December 27, 2016
    • Thank you for the code.
      I have the same problem that Sumeet Shah:
      "We have set the cookie timeout value to 20 minutes on SharePoint server for STS. So now when this code is used for some processing on the site which take more than this 20 minutes, I start getting error "The remote server returned an error: (401) Unauthorized.". Any suggestions on how to re-generate the authentication cookies once we get this error. I have tried calling the same method again to get the new instance of the ClientContext object but that does not seem to be working. It opens the browser window and this time instead of collecting the cookie and closing the window it remains opened with the my SharePoint site completely loaded in it."
      Any suggestion? Thanks in advanced.
    • Hi soyalfonsillo,
      The root cause of this issue is that the webbrowser object that is being used does not clear the cache and session data even after the object is disposed. Here our concern is FEDAUTH cookie that SharePoint sets once successful SAML token retrieval from the identity provider. To solve this issue all we need to do it get rid of this cookie once we are done with current clientcontext. Unfortunately, this is a session cookie and cannot be removed from the disc from the default cookie location at Environment.SpecialFolder.Cookies
      
      Now there is no direct way to remove session data from webbrowser object. Kindly refer below link for the guidance on how to achieve this.
      
      http://stackoverflow.com/questions/434469/how-to-clear-system-windows-forms-webbrowser-session-data
      
      we can use the suggested method call from the dispose method call of the class "ClaimsWebAuth".
      
      The dispose method would look like this :
      
       public void Dispose(bool disposing)
              {
                  if (disposing)
                  {
                      InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
      
                      if (this.webBrowser != null) this.webBrowser.Dispose();
      
                      if (this.DisplayLoginForm != null) this.DisplayLoginForm.Dispose();
                  }
              }
      
      Regards,
      Sumeet Shah
  • The remote server returned an error: (401) Unauthorized.
    1 Posts | Last post November 28, 2016
    • We have set the cookie timeout value to 20 minutes on SharePoint server for STS. So now when this code is used for some processing on the site which take more than this 20 minutes, I start getting error "The remote server returned an error: (401) Unauthorized.". Any suggestions on how to re-generate the authentication cookies once we get this error. I have tried calling the same method again to get the new instance of the ClientContext object but that does not seem to be working. It opens the browser window and this time instead of collecting the cookie and closing the window it remains opened with the my SharePoint site completely loaded in it.
      
      Kindly suggest a solution here.
  • Not able to connect to any site
    1 Posts | Last post February 19, 2015
    • I have installed SharePoint Foundation 2010 client object model redistributable as SharePoint was not installed on my system.
      I have removed the URl and added my site as "SP_Ctx https://microsoft.sharepoint.com/teams/****
      but that doesn worked and it havent returned any error.
  • I am getting below error: The requested site does not appear to have claims enabled or the Login Url has not been set.
    1 Posts | Last post November 06, 2014
    • What settings do i need to do in contasts.cs file
      
      CLAIM_HEADER_RETURN_URL
      MSG_REQUIRED_SITE_URL
      
      Where exactly i have to give SP Online site URL and ADFS URL
  • EXCEPTION
    1 Posts | Last post November 06, 2014
    • I am getting below error: 
      
      
      
      The requested site does not appear to have claims enabled or the Login Url has not been set.
      
      
      
      Do I need to have any settings done for online and how?
  • Where do you specify login/password?
    2 Posts | Last post August 28, 2013
    • I don't see how to do that.
    • The accompanying article for this sample code (http://msdn.microsoft.com/en-us/library/hh147177.aspx) highlights that the code opens a window where the User enters their credentials, and then grabs the cookie produced on successful authentication.
      
      tl;dr you don't specify the credentials in the code
  • Error : The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
    4 Posts | Last post July 03, 2013
    • Error : The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
      
      at line >> return base.Channel.EndIssue(asyncResult);
      from below code >>
      public Message EndIssue(IAsyncResult asyncResult)
              {
                  return base.Channel.EndIssue(asyncResult);
              }
      
      Above is called by below>>
       Message response = trustClient.EndIssue(
                          trustClient.BeginIssue(
                              Message.CreateMessage(
                                  MessageVersion.Default,
                                  WSTrustFeb2005Constants.Actions.Issue,
                                  new RequestBodyWriter(trustSerializer, rst)
                              ),
                              null,
                              null));
    • I am receiving the same error when connecting to the public site that has a CNAME.
      I was able to connect using the default public address which was http://stuff-web.sharepoint.com but once I used the CNAME no luck.
      
      I am currently trying to figure out why this would be.
    • Hi Raj, I have the same issue that you had described.
      
      If you have found a solution, can you please share the same with me?
    • I've got an exception at the exact same point as Raj and Ramz but my error message is different - "Authentication against the secure token server ... failed: Could not establish trust relationship for the SSL/TLS secure channel with authority ..."
      
      Strangely I only get this on SharePoint Server and not on Foundation. Anyway it turns out that the issue is to do with trust certs and once I followed the steps in this post http://www.layer2.de/en/community/FAQs/BDLC/Pages/authentication-against-the-secure-token-server.aspx everything worked as it should.
  • Error for Sharepoint Online
    3 Posts | Last post September 14, 2012
    • I am trying to use the coe with office 365 demo site . 
      I am getting below error: 
      
      The requested site does not appear to have claims enabled or the Login Url has not been set.
      
      Do I need to have any settings done for online and how?
    • I know this is an old question, but I came up with the same issue, so here's the answer I found.
      
      The readme states you have to have this installed if working from a machine that does not have SP Foundation installed on it. 
      
      http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=b4579045-b183-4ed4-bf61-dc2f0deabe47 
      
      I had the same problem until I installed this, then it worked no problem.
      
      
    • Try like this SP_Ctx https://mysite.sharepoint.com/
      
      for inner sites 
      SP_Ctx https://mysite.sharepoint.com/subsite
1 - 10 of 16 Items