I have a WCF service hosted in IIS on Domain 1. This service is accepting a request to upload a file, and then uploading the file to a SharePoint 2010 Server on Domain 2, using the client object model.

Everything works fine when I hardcode the username, password & domain using:

context.Credentials = new NetworkCredential(login, password, domain);

However, due to the compliance & security we cannot do this in our company. We need to use integrated security if possible. I have tried setting the credentials to both

context.Credentials = CredentialCache.DefaultCredentials;
context.Credentials = CredentialCache.DefaultNetworkCredentials;

But neither appear to work, we receive the error

System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate() at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

Can someone provide any tips on how to fix this? Or tips that might lead us to the resolution, logs to check etc. I am not the developer of the SharePoint site, but can chase them down. I have gone through many questions/answers here and none seem to help us.

  • What credentials do you need? Current user's or apppool's? Apr 8, 2016 at 7:48
  • app pool credentials
    – Gavin
    Apr 8, 2016 at 11:31

2 Answers 2


Set the authentication mode as shown below

context.AuthenticationMode = ClientAuthenticationMode.Default;

Also make sure the application pool account under which the service is hosted in IIS has proper access to SharePoint document library.

  • Yes I should have stated this account already has Full Control on the document library in question
    – Gavin
    Apr 7, 2016 at 9:48
  • Just to confirm this account has access, I passed the app pools identity credentials directly in and it worked,
    – Gavin
    Apr 7, 2016 at 9:58
  • I have modified the answer with correct way to set the authentication mode for client context. Apr 7, 2016 at 10:38
  • I have tried this already and does not work
    – Gavin
    Apr 7, 2016 at 11:08

It won't work with default credentials because application pool identity is not there in normal context. If you check the result of System.Security.Principal.WindowsIdentity.GetCurrent() method, you'll see it returns IUSR instead.

If you want to execute code with app pool credentials, you need to elevate permissions using SPSecurity.RunWithElevatedPrivileges().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.