Sharepoint - HTTP 401, Unauthorized using the Managed Client Object Model

Solved !! Why WPF Authentication wouldn't work when Silverlight works. (WPF was trying to use Kerberos, Silverlight was using NTLM) - Simple fix:

ClientContext _clientContext = new ClientContext(sharePointSiteUrl);
Web _web = _clientContext.Web;

_clientContext.Load(_web, website => website.Title);
_clientContext.Load(_web.Webs);

CredentialCache cc = new CredentialCache();
cc.Add(new Uri(sharePointSiteUrl), "NTLM", CredentialCache.DefaultNetworkCredentials);
_clientContext.Credentials = cc;
_clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

_clientContext.ExecuteQuery();
ListCollection _listCollection = _web.Lists;

try to use the CredentialCache (from memory) :

using (ClientContext context = new ClientContext("http://localhost"))
{
    CredentialCache cc = new CredentialCache();
    NetWorkCredential nc = new NetworkCredential("username", "password", "domain"); 

    cc.Add(new Uri("http://localhost"), "Negotiate", nc));

    context.Credentials = cc; 

    Web web = context.Web;
     context.Load(web);

    Log.Debug("Loading web.");
    context.ExecuteQuery();

    Console.WriteLine(web.Title);
}

there are also others cases where the return http code is 401, and subcode 401.1 or 401.2. Unfortunately the only way to discover them is to look inside IIS logs. the two have some causes that can be threated with different solutions. Tell us if it's the case.

[Edit] As you said you have a 401.2 error, take a look at my former answer here : 401 IIS Error for SearchAdmin.asmx. I bet it's the same.


My capability here has become functional but a lot has changed on the box since then so I won't be able to isolate which particular change resolved the issues I was experiencing.

The following considerations were relevant to the repair of this capability on my system:

  • Destroyed and re-created my web application.
  • I had a misconfigured element in my web.config. The element was inside of the element.
  • I (re-)added to the element of the web.config.

Also, I'm using the following in my console application to connect to the API:

Uri uri = new Uri("http://localhost");
using (ClientContext context = new ClientContext(uri))
{
   context.Credentials = CredentialCache.DefaultNetworkCredentials;

   Web web = context.Web;
   context.Load(web);

   context.ExecuteQuery();

   Console.WriteLine(web.Title);
}