当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - 401 Unauthorized exception throws on Sharepoint Webpart using DataContext

问题描述:

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:

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:

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??

网友答案:

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.

网友答案:

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).

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: