microsoftgraph / aspnet-snippets-sample

A repository of code snippets that use Microsoft Graph to perform common tasks such as sending email, managing groups, and other activities from an ASP.NET Core MVC app. This sample uses the Microsoft Graph .NET Client Library to work with data, and the Microsoft Identity Web Library for authentication on the Microsoft identity platform v2.0 endpoint.
MIT License
189 stars 101 forks source link

Adds MSAL-Graph auth provider support. #47

Closed peombwa closed 5 years ago

peombwa commented 5 years ago

This PR adds autorization code provider support to handle access token retreival and storage.

// Create session token provider
SessionTokenCacheProvider sessionTokenCacheProvider = new SessionTokenCacheProvider(httpContext);

// Create confidential client application
IConfidentialClientApplication cca = AuthorizationCodeProvider.CreateClientApplication(appId, redirectUri, new ClientCredential(appSecret), sessionTokenCacheProvider);

// Pass authorization code provider to GraphServiceClient
GraphServiceClient graphClient = new GraphServiceClient(new AuthorizationCodeProvider(cca, scopes));

// Make graph request
var me = await graphClient
.Me
.Events
.Request()
.WithUserAccount(ClaimsPrincipal.Current.ToGraphUserAccount())
.GetAsync();