My goal is to call REST API and retrieve specific folder and files metadata to specific site. I wrote a console application in C#. The SharePoint site is multi-factor enabled and I can get the site lists in success:
var authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext context = authenticationManager.GetWebLoginClientContext(@"https://XXX-admin.sharepoint.com/", null);
GetAllSiteCollections(context);
However I have no idea why I get credential error when calling function GetFilesPropertiesFromSite:
Message: The request was aborted: The request was canceled.
InnerException: Microsoft.SharePoint.Client.IdcrlException: Identity Client Runtime Library (IDCRL) could not look up the realm information for a federated sign-in.
at Microsoft.SharePoint.Client.Idcrl.IdcrlAuth.GetUserRealm(String login)
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)
at Microsoft.SharePoint.Client.SharePointOnlineAuthenticationModule.GetSpoAuthCookieAndUpdateRequest(WebRequest request, SharePointOnlineCredentials spoCredentials, Boolean preAuthentication)
at Microsoft.SharePoint.Client.SharePointOnlineAuthenticationModule.Authenticate(String challenge, WebRequest request, ICredentials credentials)
at System.Net.AuthenticationManagerDefault.Authenticate(String challenge, WebRequest request, ICredentials credentials)
at System.Net.AuthenticationState.AttemptAuthenticate(HttpWebRequest httpWebRequest, ICredentials authInfo)
at System.Net.HttpWebRequest.CheckResubmitForAuth()
at System.Net.HttpWebRequest.CheckResubmit(Exception& e, Boolean& disableUpload)
at System.Net.HttpWebRequest.DoSubmitRequestProcessing(Exception& exception)
at System.Net.HttpWebRequest.ProcessResponse()
at System.Net.HttpWebRequest.SetResponse(CoreResponseData coreResponseData)
Code:
private static void GetFilesPropertiesFromSite()
{
try
{
Console.WriteLine("Enter admin username: ");
string username = Console.ReadLine();
Console.WriteLine("Enter password: ");
string password = Console.ReadLine();
var Uri = new Uri(@"https://XXX.sharepoint.com/sites/AL/");
var credentials = PrepareSPOCredentials(username, password);
var result = ExecuteSearchRequest(Uri, credentials, "contentclass:STS_ListItem AND ContentType:Task");
}
catch (Exception ex)
{
Console.WriteLine("Message: " + ex.Message);
Console.WriteLine("InnerException: " + ex.InnerException);
}
}
private static SharePointOnlineCredentials PrepareSPOCredentials(string userName, string passWord)
{
var securePassWord = new SecureString();
foreach (var c in passWord.ToCharArray()) securePassWord.AppendChar(c);
var credentials = new SharePointOnlineCredentials(userName, securePassWord);
return credentials;
}
Do anyone has the same issue before? Please give me some comment/sample code. Appreciate for your help!
