Not an Issue, a suggestion for a sample
Closed this issue · 1 comments
Ofer-Gal commented
For a while, I tried to see how I could use this in my code in Azure functions where I can't use DeviceCodeProvider like:
IAuthenticationProvider ap = new DeviceCodeProvider(pca, scopes);
So instead I came up with
var clientApplication = ConfidentialClientApplicationBuilder
.Create(azureAdOptions.ClientId)
.WithTenantId(azureAdOptions.TenantId)
.WithCertificate(getCertificate(azureAdOptions.CertificatePassword))
.Build();
var SPauthenticationProvider = new ClientCredentialProvider(clientApplication, $"https://{sharepointDomain}/.default");
then
GraphServiceClient spServiceClient = new GraphServiceClient(SPauthenticationProvider, hp);
This is using a certificate in the root directory like so:
private static X509Certificate2 getCertificate(string CertificatePassword)
{
try
{
var certPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "ecgmcdev.pfx");
var certfile = System.IO.File.OpenRead(certPath);
var certificateBytes = new byte[certfile.Length];
certfile.Read(certificateBytes, 0, (int)certfile.Length);
var cert = new X509Certificate2(
certificateBytes, CertificatePassword, // HandleHooks.GetEnvironmentVariable("CertificatePassword"),
X509KeyStorageFlags.Exportable |
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet);
return cert;
}catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
If you think it is worth adding to the samples, please do.
Thank you for a great library
pschaeflein commented
There is a similar sample posted at https://cmatskas.com/create-a-net-core-deamon-app-that-calls-msgraph-with-a-certificate/
(I have yet to get @cmatskas to submit it, but I'll keep trying.)