I’m experienced in .NET but pretty new to Sharepoint and Project Online. I am curious if it’s MFA or what is going but I always get a
The remote server returned an error: (403) FORBIDDEN
For the code
public async Task<ProjectContext?> GetContextWithCredentialsAsync(string url, string username, string password)
{
try
{
if (url.ToUpper().StartsWith("HTTP://") || url.ToUpper().StartsWith("HTTPS://"))
{
var securePassword = new SecureString();
password.ToCharArray().ToList().ForEach(s => securePassword.AppendChar(s));
var clientContext = new ProjectContext(url)
{
Credentials = new NetworkCredential(username, securePassword)
};
clientContext.Load(clientContext.Projects);
clientContext.ExecuteQuery();
return await System.Threading.Tasks.Task.Factory.StartNew(() => { return clientContext; });
}
}
catch (Exception ex)
{
throw ex;
}
return null;
}
I have seen other similar code online doing this and I think maybe it is MFA and I need to get an auth but other links I have seen were older .NET. I am using the library: ‘Microsoft.SharePointOnline.CSOM’ version 16.1.24810.12000 in a .NET 8 Web API calling the code above.