I am trying to access SharePoint site using CSOM with Windows Authentication. It works if I run it in Visual Studio on a server. However, it returns a 401 unauthorized when I run the site through IIS on a server.

I have the followings set in web.config:

<authentication mode="Windows" />

<identity impersonate="true" />

I have the followings ENABLED in IIS:

"ASP.NET Impersonation" = ENABLED

"Windows Authentication = "ENABLED"

I am not sure what I am missing here. I have tried many different ways but no lucks yet.

Here is my code snippet:

using (ClientContext clientContext = new ClientContext(spUri))
    clientContext.Credentials = CredentialCache.DefaultNetworkCredentials; 
    Microsoft.SharePoint.Client.Web site = clientContext.Web;
    clientContext.ExecuteQuery(); //THIS IS WHERE IT FAILS

Following code will work for sure.

using (ClientContext context = new ClientContext("http://siteURL/")) 
    context.Credentials = new NetworkCredential("user", "password", "domain");
    Microsoft.SharePoint.Client.Web site = context.Web;


  • Well yes, but it will not help in using windows authentication based on the logged in user Sep 19 '18 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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