Last active
December 15, 2016 07:39
-
-
Save chrisobriensp/9e2165cf67c602330236 to your computer and use it in GitHub Desktop.
Authentication to Office 365 APIs using the Office 365 .NET client library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // code to authenticate to Office 365 and obtain a DiscoveryClient object, which can then be used to create | |
| // a SharePointClient or OutlookServicesClient in order to access files/mail/calendar/contacts etc.. | |
| var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; | |
| var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; | |
| // !!! NOTE: DO NOT USE NaiveSessionCache IN PRODUCTION. A MORE PERSISTENT CACHE SUCH AS A DATABASE IS RECOMMENDED FOR PRODUCTION USE !!!! | |
| AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new NaiveSessionCache(signInUserId)); | |
| DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri, | |
| async () => | |
| { | |
| var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId, | |
| new ClientCredential(SettingsHelper.ClientId, | |
| SettingsHelper.AppKey), | |
| new UserIdentifier(userObjectId, | |
| UserIdentifierType.UniqueId)); | |
| return authResult.AccessToken; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment