SharePoint Dragons

Nikander & Margriet on SharePoint

Authentication when using the SharePoint client object model

Normally, when you need to log in using a specific credential set in the SharePoint client object model, you’ll have to provide the correct credentials to authenticate to the SharePoint site collection, like so:

NetworkCredential credentials = new NetworkCredential(“username”, “pwd”, “domain”);

ClientContext context = new ClientContext(“http://thesitecollection”);

context.Credentials = credentials;

This won’t work if you have a claims based site set up supporting both Windows and forms authentication. See https://sharepointdragons.com/2012/01/30/claims-in-sharepoint-2010/ for more about setting that up. Instead, you need to set up the appropriate HTTP headers to disable Forms authentication, and it’ll work again:

ClientContext clientContext = new ClientContext(“http://thesitecollection“);clientContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(clientContext_ExecutingWebRequest);

Web site = clientContext.Web;

clientContext.Load(site);

clientContext.ExecuteQuery();

 

static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e){

    e.WebRequestExecutor.WebRequest.Headers.Add(“X-FORMS_BASED_AUTH_ACCEPTED”, “f”);

}

See http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/16cd0e26-8f3b-4ef2-bac4-c2c59849ab96 for more info.

Advertisements

6 responses to “Authentication when using the SharePoint client object model

  1. Pingback: Forms authentication using the Client Object Model « SharePoint Dragons

  2. Pavan September 12, 2012 at 11:22 am

    Thanks, this was what I was looking for

  3. LyLV March 21, 2013 at 4:33 am

    LyLV: Thanks so much!

  4. Kyle January 29, 2015 at 12:30 am

    Great quick find, thanks much..

  5. Andrew Weddle June 30, 2015 at 3:36 am

    Life saver. This has been irritating me for a few days off and on!

  6. ltd July 17, 2015 at 2:18 am

    Awesome!! I struggled for hours getting the 403 Forbidden error however this fixed it! Many thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: