Skip to content

Instantly share code, notes, and snippets.

@SibeeshVenu
Last active February 5, 2021 09:00
Show Gist options
  • Save SibeeshVenu/821028ee794b70f893c7e3cab379a0fa to your computer and use it in GitHub Desktop.
Save SibeeshVenu/821028ee794b70f893c7e3cab379a0fa to your computer and use it in GitHub Desktop.
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
namespace < YOURNAMESPACE > {
public interface IGraphAuthService {
GraphServiceClient GetGraphServiceClient(Guid tenantId);
}
public class GraphAuthService: IGraphAuthService {
private readonly IConfiguration _configuration;
public GraphAuthService(IConfiguration configuration) {
_configuration = configuration;
}
private ClientCredentialProvider GetGraphAuthProvider(Guid tenantId) {
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(_configuration["AzureAd:ClientId"])
.WithTenantId(Convert.ToString(tenantId))
.WithClientSecret(_configuration["GraphClientSecret"])
.Build();
return new ClientCredentialProvider(confidentialClientApplication);
}
public GraphServiceClient GetGraphServiceClient(Guid tenantId) = >new GraphServiceClient(GetGraphAuthProvider(tenantId));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment