GetAuthenticationCookie 返回的 SharePoint Online 问题 "Value cannot be null. Parameter name: cookieHeader"

SharePoint Online Issue with GetAuthenticationCookie returning "Value cannot be null. Parameter name: cookieHeader"

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

Value cannot be null. Parameter name: cookieHeader

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

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 在线管理 Shell 检查了“LegacyAuthProtocolsEnabled”的值,它已经是真的。

$TenantSettings = Get-SPOTenant
$TenantSettings.LegacyAuthProtocolsEnabled

我在某个时候开始遇到同样的错误。这显然是 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));
    ...
    ...
    ...