We have SharePoint 365 site with MFA enabled. One project folder on the SharePoint contains an Excel file that I want to read and process via my unattended C# application. Interactively I can open the file after going through the MFA process. I tried following 5 cases but each one throws an exception:
The URL of the file is:
string excelUrl = @"https://company.sharepoint.com/teams/Projects/DocCtrl/Project.xlsm";
For each of these 5 cases, the web client headers are (showing here to avoid repetitions in the subsequent code):
webClient.Headers[HttpRequestHeader.UserAgent] = "Other";
webClient.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";
Case-1:
using (WebClient webClient = new WebClient())
{
webClient.UseDefaultCredentials = true;
using (MemoryStream memoryStream = new MemoryStream(webClient.DownloadData(excelUrl)))
using (SpreadsheetDocument xlDocument = SpreadsheetDocument.Open(memoryStream, false))
readAndProcess(xlDocument);
}
Exception on webClient.DownloadData(excelUrl):
The remote server returned an error: (401) Unauthorized.
Case-2:
I would like to avoid this becuase it needs password to be either hardcoded or managed in some configuration file.
using (WebClient webClient = new WebClient())
{
webClient.Credentials = new NetworkCredential(@"emailId@company.com", @"Password", "Domain");
using (MemoryStream memoryStream = new MemoryStream(webClient.DownloadData(excelUrl)))
using (SpreadsheetDocument xlDocument = SpreadsheetDocument.Open(memoryStream, false))
readAndProcess(xlDocument);
}
Exception on webClient.DownloadData(excelUrl):
The remote server returned an error: (401) Unauthorized.
Case-3:
I would like to avoid this becuase it needs password to be either hardcoded or managed in some configuration file.
using (WebClient webClient = new WebClient())
{
SecureString securePwd = new SecureString();
foreach (char chr in @"Password")
securePwd.AppendChar(chr);
webClient.Credentials = new SharePointOnlineCredentials(@"emailId@company.com", securePwd);
using (MemoryStream memoryStream = new MemoryStream(webClient.DownloadData(excelUrl)))
using (SpreadsheetDocument xlDocument = SpreadsheetDocument.Open(memoryStream, false))
readAndProcess(xlDocument);
}
Exception on webClient.DownloadData(excelUrl):
The request was aborted: The request was canceled.
The partner returned a bad sign-in name or password error. For more information, see Federation Error-handling Scenarios.
Case-4:
This does prompt me for the user id, password, and MFA. I would like to avoid such solution because my application is running from unattended console.
using (WebClient webClient = new WebClient())
{
var authManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext ctx = authManager.GetWebLoginClientContext(@"https://company.sharepoint.com");
ctx.Load(ctx.Web, w => w.Title);
ctx.ExecuteQuery();
using (MemoryStream memoryStream = new MemoryStream(webClient.DownloadData(excelUrl)))
using (SpreadsheetDocument xlDocument = SpreadsheetDocument.Open(memoryStream, false))
readAndProcess(xlDocument);
}
Exception on webClient.DownloadData(excelUrl):
The remote server returned an error: (401) Unauthorized.
Case-5:
This also prompt me for the user id, password, and MFA, even though it is specified in the code. I would like to avoid such solution because my application is running from unattended console.
using (WebClient webClient = new WebClient())
{
var authManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext ctx = authManager.GetWebLoginClientContext(@"https://company.sharepoint.com");
SecureString securePwd = new SecureString();
foreach (char chr in @"Password")
securePwd.AppendChar(chr);
ctx.Credentials = new SharePointOnlineCredentials(@"emailId@company.com", securePwd);
ctx.Load(ctx.Web, w => w.Title);
ctx.ExecuteQuery();
using (MemoryStream memoryStream = new MemoryStream(webClient.DownloadData(excelUrl)))
using (SpreadsheetDocument xlDocument = SpreadsheetDocument.Open(memoryStream, false))
readAndProcess(xlDocument);
}
Exception on ctx.ExecuteQuery():
The partner returned a bad sign-in name or password error. For more information, see Federation Error-handling Scenarios.
Any suggestions on how to access an Excel from MFA enabled SharePoint 365 site from unattended C# application? Would app password help in such cases?