Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Last active December 15, 2016 07:39
Show Gist options
  • Select an option

  • Save chrisobriensp/9e2165cf67c602330236 to your computer and use it in GitHub Desktop.

Select an option

Save chrisobriensp/9e2165cf67c602330236 to your computer and use it in GitHub Desktop.
Authentication to Office 365 APIs using the Office 365 .NET client library
// 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