无法从.NET Core验证SharePoint REST或CSOM调用

时间:2018-10-17 11:16:15

标签: c# sharepoint .net-core sharepoint-online csom

我在从.NET Core控制台应用程序使用CSOM和REST服务访问SharePoint时遇到问题。

首先,我创建了一个针对.NET Framework 4.6.1的控制台应用程序,安装了Microsoft.SharePointOnline.CSOM nuget程序包,并添加了两个示例方法以使用硬编码的用户凭据连接到SharePoint。这行得通。

public static void CsomCall()
{
    var password = "password";
    var securePassword = new SecureString();
    foreach (var c in password.ToCharArray()) securePassword.AppendChar(c);

    using (ClientContext context = new ClientContext("https://siteurl"))
    {
        context.Credentials = new SharePointOnlineCredentials("user@domain", securePassword);
        Web web = context.Web;
        context.Load(web);
        context.ExecuteQuery();
        Console.WriteLine(web.Title);
    }
}
private static void RestCall()
{
    var password = "password";
    var securePassword = new SecureString();
    foreach (var c in password.ToCharArray()) securePassword.AppendChar(c);

    var credentials = new SharePointOnlineCredentials("user@domain", securePassword);

    using (WebClient client = new WebClient())
    {
        client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
        client.Credentials = credentials;
        client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
        client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
        var content = client.DownloadString("https://siteurl/_api/web/lists");
        Console.WriteLine(content);
    }
}

然后,我创建了一个.NET Core控制台应用程序,并通过上述方法进行了复制。我知道.NET Core尚不支持SharePoint CSOM,因此我使用了建议的解决方法here,并手动引用了Microsoft.SharePoint.Client.Portable.dll,Microsoft.SharePoint.Client.Runtime.Portable.dll和Microsoft.SharePoint.Client.Runtime.Windows.dll。

我必须进行一些更改才能编译代码:

  • .ExecuteQueryAsync()而不是.ExecuteQuery()

  • var credentials = new SharePointOnlineCredentials("user@domain", password);注意,密码是Microsoft.SharePoint.Client.Runtime.Portable中的纯字符串。

运行CsomCall时,访问网络失败。标题:

  

Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException:'属性或字段'Title'尚未初始化。尚未请求或尚未执行请求。可能需要明确提出要求。'

运行RestCall时,它将失败,并在client.DownloadString上显示错误:

  

System.Net.WebException:'远程服务器返回错误:(401)未经授权。'

是否可以设置SharePointOnlineCredentials以与.NET Core一起使用?对堆栈溢出的搜索表明这应该可行,但我似乎无法使其正常工作。

最终,我们希望使用ASP.NET Core构建一个Web API服务,以为内部用户生成文档(从SharePoint中读取文档模板,生成新文档并保存回SharePoint)。使用CSOM或REST,并且现在可以在Windows操作系统上运行。

1 个答案:

答案 0 :(得分:-1)

REST调用是错误的。您必须使用凭据获得令牌。另外,大多数不推荐使用WebClient,请改用HttpClient类。

看这个例子:

public const string BaseUri = "https://example.sharepoint.com";
private static HttpClient _client;

public static void Initialize()
{
    SharePointOnlineCredentials currentCredentials = GetCredentialsHere();

    var handler = new HttpClientHandler
    {
        Credentials = currentCredentials
    };

    _client = new HttpClient(handler);

    // you are missing this line
    handler.CookieContainer.SetCookies(BaseUri, currentCredentials.GetAuthenticationCookie(BaseUri));
    _client.BaseAddress = BaseUri;
    _client.DefaultRequestHeaders.Clear();
    _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    _client.MaxResponseContentBufferSize = 2147483647;
}