public static async Task <SharePointClient> EnsureClientCreated() { if (_discoveryContext == null) { _discoveryContext = await DiscoveryContext.CreateAsync(); } ResourceDiscoveryResult dcr = null; try { dcr = await _discoveryContext.DiscoverResourceAsync(ServiceResourceId); } catch (AuthenticationFailedException) { // If the user go back in the login page an Authentication exception is launched. return(null); } _lastLoggedInUser = dcr.UserId; return(new SharePointClient(ServiceEndpointUri, async() => { return (await _discoveryContext.AuthenticationContext .AcquireTokenSilentAsync(ServiceResourceId, _discoveryContext.AppIdentity.ClientId, new Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier(dcr.UserId, Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType.UniqueId))).AccessToken; })); }
private async Task <string> GetAccessToken() { string ServiceResourceId = "https://outlook.office365.com"; Uri ServiceEndpointUri = new Uri("https://outlook.office365.com/ews/odata"); DiscoveryContext disco = await DiscoveryContext.CreateAsync(); // this triggers user login prompt for Office 365 ResourceDiscoveryResult rdr = await disco.DiscoverResourceAsync(ServiceResourceId); string clientId = disco.AppIdentity.ClientId; string clientSecret = disco.AppIdentity.ClientSecret; string refreshToken = new SessionCache().Read("RefreshToken"); ClientCredential creds = new ClientCredential(clientId, clientSecret); AuthenticationResult authResult = await disco.AuthenticationContext.AcquireTokenByRefreshTokenAsync(refreshToken, creds, ServiceResourceId); return(authResult.AccessToken); }
private async Task <ExchangeClient> EnsureClientCreated() { // get or create DiscoveryContext DiscoveryContext disco = GetFromCache("DiscoveryContext") as DiscoveryContext; if (disco == null) { disco = await DiscoveryContext.CreateAsync(); SaveInCache("DiscoveryContext", disco); } // obtain ResourceDiscoveryResult for Exchange string ServiceResourceId = "https://outlook.office365.com"; ResourceDiscoveryResult dcr = await disco.DiscoverResourceAsync(ServiceResourceId); SaveInCache("LastLoggedInUser", dcr.UserId); string clientId = disco.AppIdentity.ClientId; string clientSecret = disco.AppIdentity.ClientSecret; Uri ServiceEndpointUri = new Uri("https://outlook.office365.com/ews/odata"); // create ExchangeClient object with callback function for obtaining access token ExchangeClient exchangeClient = new ExchangeClient(ServiceEndpointUri, async() => { AuthenticationContext authContext = disco.AuthenticationContext; ClientCredential creds = new ClientCredential(clientId, clientSecret); UserIdentifier userId = new UserIdentifier(dcr.UserId, UserIdentifierType.UniqueId); // execute call across network to acquire access token AuthenticationResult authResult = await authContext.AcquireTokenSilentAsync(ServiceResourceId, creds, userId); // return access token to caller as string value return(authResult.AccessToken); }); // return new ExchangeClient to caller return(exchangeClient); }