Im facing probelem in initializing GraphServiceClient with the accessToken in my request in Microsoft.Graph v5 sdk
This is Microsoft.Graph v4 implementation. Here I'm trying to invite a user to my entra id through an asp.net core web api (.net 8) . I'm creating GraphServiceClient with the accessToken i received in my request. This is working fine but the problem is now we have Microsoft.Graph v5 which is latest and v4 is outdated and in v5 we dont have DelegateAuthenticationProvider. Kindly help me out in updating same implementation v5 sdk.
var authorizationHeader = HttpContext.Request.Headers["Authorization"].FirstOrDefault();
var accessToken = authorizationHeader.Substring("Bearer ".Length);
var _graphServiceClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
requestMessage =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.FromResult(0);
}));
var invitation = new Invitation
{
InvitedUserEmailAddress = userEmail,
SendInvitationMessage = true,
InviteRedirectUrl = "https://loremipsum.app",
InvitedUserType = "guest" // default is guest,member
};
var invite = await _graphServiceClient.Invitations
.Request()
.AddAsync(invitation);
return Ok(invite);
In your case, I would implement
IAccessTokenProviderWhen creating a new instance of
GraphServiceClient, useBaseBearerTokenAuthenticationProviderwith yourTokenProviderCode
Assume you are using Azure.Identity and Microsoft.Graph NuGet packages.