1
votes

I'm using the below code to create a new instance of Sharepoint datacontext:

spDataContext con = new spDataContext(new Uri("http://mysite/subsite/_vti_bin/ListData.svc"));
con.Credentials = CredentialCache.DefaultCredentials;

Then I try fetching the items by using

var result = con.testingList.ToList();

Then Below error shows:

"401 UNAUTHORIZED"

Here's the screenshot of the error:

enter image description here

But if I change the credentials by providing the sharepoint admin username and password, I am able to fetch the items.

con.Credentials = new NetworkCredential(username, password);

But the problem is: I want to use the current user's credentials instead of administrator account, since not all the items are supposed to be seen by current user.

Some addition information from the IIS:

enter image description here

At first, all authentication methods are enabled, then I tried turning off basic and digest authentication to see if it helps, but the exception still throws.

So how to fix this problem??

3

3 Answers

1
votes

Check what account you are actually connecting with by inspecting a breakpoint, writing to a log or checking the server's audit trail/Logs (IIS/SharePoint) - are you sure it's a valid account and not an IUSR_xyz IIS account or something like that? Also check that you are not hitting a Kerberos "double hop" if you are working with a distributed architecture. This can often trip you up when adding another server/service to the mix.

1
votes

In order to get this to work, you should configure the Authentication options in IIS as follows:

  • Anonymous Authentication - DISABLED
  • ASPNET Impersonation - DISABLED
  • Windows Authentication - ENABLED

You can use the CredentialCache.DefaultCredentials, but I would suggest switching from using the WCF services to using CSOM (Microsoft.SharePoint.Client).

1
votes

At last the reason was:

I don't have the System.Web assembly in the class library, so the system was unable to get the CredentialCache.DefaultCredentials.

enter image description here