【问题标题】:Sharepoint authentication in C# not workingC# 中的 Sharepoint 身份验证不起作用
【发布时间】:2014-05-05 21:49:58
【问题描述】:

我正在尝试通过创建 C# 代码来自动化 sharepoint 上的一些琐碎任务。我认为代码是正确的,但是在运行它时,我收到一个 401 错误,指出“远程服务器返回错误:(401)未经授权。”

我在代码中使用的用户凭据对相关列表具有完全控制访问权限,并且可以通过 UI 执行代码中的任务。这是我第一次编写 C#,所以任何帮助将不胜感激!

using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;


namespace ConsoleApplication3
{
class Program
{
    static void Main()
    {
        ClientContext context = new ClientContext("websiteURL");
        context.Credentials = new NetworkCredential("user", "pasword", "domain");
        List oList = context.Web.Lists.GetByTitle("TestBI");
        ListItem oListItem = oList.GetItemById(0);

        for (int i = 0; i < 3; i++)
        {
            oListItem = oList.GetItemById(i);
            oListItem.BreakRoleInheritance(true, false);
        }
        context.ExecuteQuery();
    }
}
}

【问题讨论】:

  • 您是否使用域帐户登录 SharePoint?
  • 不是普通用户,但可以完全控制列表和子站点

标签: c# authentication sharepoint


【解决方案1】:

由于您没有使用域帐户,因此创建 NetworkCredential 的方式应该是:

context.Credentials = new NetworkCredential("user", "pasword");

【讨论】:

  • 我试过了,但不是 401:未经授权的错误,而是在 context.ExecuteQuery() 行上出现“项目不存在。它可能已被另一个用户删除”错误跨度>
  • 没关系,你的建议奏效了,第二个错误是我修复的代码有问题。非常感谢!
猜你喜欢
  • 2014-09-04
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 2020-09-23
  • 2012-04-29
  • 2012-03-18
相关资源
最近更新 更多