Hi @Ted Wagner ,
Below is my sample for you to get the MSAL access token
private static async Task<string> GetToken()
{
string applicationId = "client-id";
string tenantId = "tenant.onmicrosoft.com";
X509Certificate2 certificate = new X509Certificate2(@"C:\cer.pfx", "password");
IConfidentialClientApplication confApp = ConfidentialClientApplicationBuilder.Create(applicationId)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.WithCertificate(certificate)
.Build();
var scopes = new[] { "https://tenant.sharepoint.com/.default" };
var authenticationResult = await confApp.AcquireTokenForClient(scopes).ExecuteAsync();
return authenticationResult.AccessToken;
}
static async Task Main(string[] args)
{
string site = "https://tenant.sharepoint.com/sites/test";
string token = await GetToken();
Console.WriteLine(token);
ClientContext ctx = new ClientContext(site);
ctx.ExecutingWebRequest += (s, e) =>
{
e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + token;
};
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
Console.WriteLine(web.Title);
}
Here is a nice blog to use CSOM with an MSAL toke for your reference: https://www.vrdmn.com/2020/06/using-net-standard-csom-and-msalnet-for.html
And I would suggest use PnP Framework library directly for SharePoint CSOM. This is much easier. For example, you just need to get the clientcontext like below.
var context = new PnP.Framework.AuthenticationManager(clientId, certificatePath, password, "tenant.onmicrosoft.com").GetContext(site);
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.