I followed this article but I modified the sample code a bit to use GetContextAsync instead of GetAzureADAppOnlyAuthenticatedContext, I get the error "The remote server returned an error: (401) Unauthorized." every single time.
AuthenticationManager authManager = new AuthenticationManager(clientId, certPath,certPassword,tenantId);
using (ClientContext cc = await authManager.GetContextAsync(_siteUrl))
{
cc.Load(cc.Web, p => p.Title);
await cc.ExecuteQueryAsync();
Console.WriteLine(cc.Web.Title);
}
The error is thrown at await cc.ExecuteQueryAsync();
I have uploaded the self-signed certificate onto Azure portal
and granted the permission
My app is a Winforms app using .NET framework 4.7 PnP.Framework 1.11
P/s: What do I enter for the tenantId param? At the moment I'm using the Directory (tenant) ID from the Overview page
I have tried to reproduce the same in my environment.
It is then being uploaded to my app registration
Checked the manifest to confirm
using postman
code given:
Try using
if-elseandtry-catchblock to catch the exact error.As
getcontextuses current users credentials , but here we are intended to use app only context.If users login is not having privileges to access the sharepoint or when entered wrong Login details , the 401 unauthorized usually occurs.
If the user profile needs to be read , it needs
user.readpermission.But note the limitation Accessing SharePoint using an application context, also known as app-only | Microsoft Learn here.
Reference : azure active directory - SharePoint PnP AuthenticationManager login with current user - Stack Overflow