private IGraphServiceClient BuildClient(AuthenticationResult authenticationResult) { logDebug("buildClient..."); //DeviceCodeProvider authenticationProvider = new DeviceCodeProvider(_publicClientApp, Scopes); var authenticationProvider = new DelegateAuthenticationProvider( (requestMessage) => { var access_token = authenticationResult.AccessToken; requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", access_token); return(Task.FromResult(0)); }); GraphServiceClientWithState clientWithState = new GraphServiceClientWithState() { Client = new GraphServiceClient(authenticationProvider), RequiresUserInteraction = false, TokenExpiryDate = authenticationResult.ExpiresOn.LocalDateTime }; if (authenticationResult.Account == null) { throw new Exception("authenticationResult.Account == null!"); } mClientByUser[authenticationResult.Account.HomeAccountId.Identifier] = clientWithState; return(clientWithState.Client); }
private async Task <IGraphServiceClient> TryGetMsGraphClient(String path, bool tryConnect) { String userId = OneDrive2ItemLocation <OneDrive2PrefixContainerType> .FromString(path).User.Id; logDebug("TryGetMsGraphClient for " + userId); if (mClientByUser.ContainsKey(userId)) { logDebug("TryGetMsGraphClient found user " + userId); GraphServiceClientWithState clientWithState = mClientByUser[userId]; if (!(clientWithState.RequiresUserInteraction || (clientWithState.TokenExpiryDate < DateTime.Now) || (clientWithState.Client == null))) { logDebug("TryGetMsGraphClient returning client"); return(clientWithState.Client); } else { logDebug("not returning client because " + clientWithState.RequiresUserInteraction + " " + (clientWithState.TokenExpiryDate < DateTime.Now) + " " + (clientWithState.Client == null)); } } if (tryConnect) { logDebug("trying to connect..."); if (await TryLoginSilent(path) != null) { logDebug("trying to connect ok"); return(mClientByUser[userId].Client); } logDebug("trying to connect failed"); } logDebug("TryGetMsGraphClient for " + userId + " returns null"); return(null); }
private async Task <string> TryLoginSilent(string iocPath) { IAccount account = null; try { if (IsConnected(iocPath)) { return(iocPath); } String userId = OneDrive2ItemLocation <OneDrive2PrefixContainerType> .FromString(iocPath).User?.Id; logDebug("needs acquire token"); logDebug("trying silent login " + iocPath); account = Task.Run(async() => await _publicClientApp.GetAccountAsync(userId)).Result; logDebug("getting user ok."); } catch (Exception e) { logDebug(e.ToString()); } if (account != null) { try { logDebug("AcquireTokenSilent..."); AuthenticationResult authResult = await _publicClientApp.AcquireTokenSilent(Scopes, account) .ExecuteAsync(); logDebug("AcquireTokenSilent ok."); BuildClient(authResult); /*User me = await graphClient.Me.Request().WithForceRefresh(true).GetAsync(); * logDebug("received name " + me.DisplayName);*/ return(BuildRootPathForUser(authResult)); } catch (MsalUiRequiredException ex) { GraphServiceClientWithState clientWithState = new GraphServiceClientWithState() { Client = null, RequiresUserInteraction = true }; mClientByUser[account.HomeAccountId.Identifier] = clientWithState; logDebug("ui required"); return(null); } catch (Exception ex) { logDebug("silent login failed: " + ex.ToString()); return(null); } } return(null); }
private async Task <IGraphServiceClient> TryGetMsGraphClient(String path, bool tryConnect) { String userId = OneDrive2ItemLocation <OneDrive2PrefixContainerType> .FromString(path).User.Id; if (mClientByUser.ContainsKey(userId)) { GraphServiceClientWithState clientWithState = mClientByUser[userId]; if (!(clientWithState.RequiresUserInteraction || (clientWithState.TokenExpiryDate < DateTime.Now) || (clientWithState.Client == null))) { return(clientWithState.Client); } } if (tryConnect) { if (await TryLoginSilent(path) != null) { return(mClientByUser[userId].Client); } } return(null); }