I have a .net web api core project and I will call microsoft graph
So I created a configuration class:
public class GraphConfiguration
{
public static void Configure(IServiceCollection services, IConfiguration configuration)
{
//Look at appsettings.Development.json | https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1
var graphConfig = new AppSettingsSection();
configuration.GetSection("AzureAD").Bind(graphConfig);
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(graphConfig.ClientId)
.WithTenantId(graphConfig.TenantId)
.WithClientSecret(graphConfig.ClientSecret)
.Build();
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);
}
}
and in my controller I have this:
public class UserController : ControllerBase
{
private TelemetryClient telemetry;
private readonly ICosmosStore<Partner> _partnerCosmosStore;
private readonly GraphServiceClient _graphServiceClient;
// Use constructor injection to get a TelemetryClient instance.
public UserController(TelemetryClient telemetry,ICosmosStore<Partner> partnerCosmosStore, GraphServiceClient graphServiceClient)
{
this.telemetry = telemetry;
_partnerCosmosStore = partnerCosmosStore;
_graphServiceClient = graphServiceClient;
}
/// <summary>
/// Gets all partners
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<ActionResult> GetUsers()
{
this.telemetry.TrackEvent("GetPartners");
try
{
var me = await _graphServiceClient.Me.Request().WithForceRefresh(true).GetAsync();
return Ok(me);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
}
But I think I am missing a step here, in Startupcs. how do I actually inject it in all controllers?