GetAuthenticationCookie 返回“值不能为空。参数名称:cookieHeader”的 SharePoint Online 问题

时间:2021-04-28 12:00:00

标签: azure sharepoint sharepoint-online

我通过 API 接口对 SharePoint Online 用户进行身份验证。在过去的几年里,它一直运行良好。但是从星期一开始我就收到错误

<块引用>

值不能为空。参数名称:cookieHeader

有一个代码可以在 Azure 上托管的 API 中为 SharePoint Online 生成 HTTPClientHandler。

HttpClientHandler result = new HttpClientHandler();
try
{

    SecureString securePassword = new SecureString();

    foreach (char c in userPassword) { securePassword.AppendChar(c); }

    SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(userName, securePassword);

    result.Credentials = credentials;

    string authCookieValue = credentials.GetAuthenticationCookie(new Uri(hostWebURL));

    result.CookieContainer.SetCookies(new Uri(hostWebURL), authCookieValue);

}
catch (Exception ex)
{
    throw ex;
}

return result;

在上面的代码行中,我们得到了‘authCookieValue’的空值。

我还使用以下命令通过 SharePoint Online Management Shell 检查了“LegacyAuthProtocolsEnabled”的值,它已经是正确的。

$TenantSettings = Get-SPOTenant
$TenantSettings.LegacyAuthProtocolsEnabled

1 个答案:

答案 0 :(得分:0)

我在某个时候开始遇到同样的错误。这显然是 Sharepoint Online 方面的变化,因为多年来我方面没有任何变化。

我认为为我修复它的关键是:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

见下文:

public async Task<List<SharepointDocumentDetail>> GetFileInfoFromSharepoint(string specificFolderUrl)
{
    // Once I added this, the error went away
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    List<SharepointDocumentDetail> ret = new List<SharepointDocumentDetail>();
     
    var restUrl = string.Format("{0}/_api/web/GetFolderByServerRelativeUrl('{1}')/Files?$expand=Files", this.Sharepoint_DocRootUrl, specificFolderUrl);
   
    //Creating Credentials
    var passWord = new SecureString();
    foreach (var c in this.Sharepoint_Password) passWord.AppendChar(c);
     
    var credential = new SharePointOnlineCredentials(this.Sharepoint_User, passWord);
    
    using (var handler = new HttpClientHandler() { Credentials = credential })
    { 
        Uri uri = new Uri(this.Sharepoint_DocRootUrl);
         
        // I was getting the error on this line
        handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));
    ...
    ...
    ...
相关问题