0

I am using below piece of code to access SharePoint Online lists web services and receiving the exception at GetAuthenticationCookie method.

Please note that this code is working for some SharePoint tenancies and not working for some. I have already tried with both 16 version and 15 version client and client run time dlls and there is no difference in output.

Is there any setting that need to be checked/changed in SharePoint Online site/tenancy?

Code:

var securePassword = new SecureString();

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

var credentials = new SharePointOnlineCredentials(userName, securePassword);

var authCookie = credentials.GetAuthenticationCookie(webUri);

Exception:

System.Xml.XmlException: 'center' is an unexpected token. The expected token is '"' or '''. Line 7, position 12. at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args) at System.Xml.XmlTextReaderImpl.ParseAttributes() at System.Xml.XmlTextReaderImpl.ParseElement() at System.Xml.XmlTextReaderImpl.ParseElementContent() at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r) at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.DoGet(String url) at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.RequestFederationProviderInfo(String domainname) at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetFederationProviderInfo(String domainname) at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.InitFederationProviderInfoForUser(String username) at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetServiceToken(String username, String password, String serviceTarget, String servicePolicy) at Microsoft.SharePoint.Client.Idcrl.SharePointOnlineAuthenticationProvider.GetAuthenticationCookie(Uri url, String username, SecureString password, Boolean alwaysThrowOnFailure, EventHandler`1 executingWebRequest) at Microsoft.SharePoint.Client.SharePointOnlineCredentials.GetAuthenticationCookie(Uri url, Boolean refresh, Boolean alwaysThrowOnFailure)

1 Answer 1

1

First, make sure that you're not using multi-factor authentication for the SharePoint user and that the 'LegacyAuthProtocolsEnabled' property is set to True for the tenant: https://blogs.technet.microsoft.com/sharepointdevelopersupport/2018/10/27/sharepoint-online-authentication-in-powershell-for-csom-when-legacy-authentication-is-disabled-for-tenant-or-multi-factor-authentication-is-enabled-for-user/

Next, enforce using Tls 1.2 by your HTTP-client with the code:

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

Place it before creating SharePointOnlineCredentials

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.