From ecb7e3ab2a2b79c016949de093b9d325b7236e1f Mon Sep 17 00:00:00 2001 From: Mikael Svenson Date: Sun, 18 Apr 2021 17:39:27 +0200 Subject: [PATCH] Added input field for optional tenant id --- .../SearchQueryTool/MainWindow.xaml | 31 ++++++++++------ .../SearchQueryTool/MainWindow.xaml.cs | 36 +++++++++++-------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml b/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml index 4a3e5419..ed55381f 100644 --- a/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml +++ b/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml @@ -1123,6 +1123,7 @@ + @@ -1135,26 +1136,40 @@ LostFocus="SharePointSiteUrlTextBox_LostFocus" TextAlignment="Left" /> + + + + - + - + - + @@ -1220,15 +1235,9 @@ - - - - - - - + diff --git a/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml.cs b/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml.cs index 900f5d34..538f8070 100644 --- a/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml.cs +++ b/Solutions/SharePoint.Search.QueryTool/SearchQueryTool/MainWindow.xaml.cs @@ -61,6 +61,7 @@ private enum PropertyType private bool _enableExperimentalFeatures; private bool _firstInit = true; private static string _adalToken = null; + private static InteractiveAuthenticationProvider _interactiveProvider; public SearchPresetList SearchPresets { get; set; } private string PresetFolderPath { get; set; } @@ -506,29 +507,36 @@ async Task AdalLogin(bool forcePrompt) const string clientId = "9bc3ab49-b65d-410a-85ad-de819febfddc"; const string redirectUri = "https://oauth.spops.microsoft.com/"; - if (forcePrompt || IsExpired(_adalToken, spUri.Host)) - { - string tenant = spUri.Host.Replace("sharepoint", "onmicrosoft") + string tenant = spUri.Host.Replace("sharepoint", "onmicrosoft") .Replace("-df", "") .Replace("-admin", ""); - InteractiveAuthenticationProvider interactiveProvider = new InteractiveAuthenticationProvider(clientId, tenant, new Uri(redirectUri)); - _adalToken = await interactiveProvider.GetAccessTokenAsync(resourceUri); + if(!string.IsNullOrWhiteSpace(TenantIdTextBox.Text)) + { + tenant = TenantIdTextBox.Text.Trim(); } - _searchQueryRequest.Token = _searchSuggestionsRequest.Token = "Bearer " + _adalToken; - } - static bool IsExpired(string token, string host) - { - if (string.IsNullOrWhiteSpace(token)) + if (_interactiveProvider == null || _interactiveProvider.TenantId != tenant) { - return true; + _interactiveProvider = new InteractiveAuthenticationProvider(clientId, tenant, new Uri(redirectUri)); } - var jwtToken = new JwtSecurityToken(token); - if (jwtToken.Audiences.First().Contains(host) == false) return true; - return jwtToken.ValidTo < DateTime.UtcNow; + + _adalToken = await _interactiveProvider.GetAccessTokenAsync(resourceUri); + + _searchQueryRequest.Token = _searchSuggestionsRequest.Token = "Bearer " + _adalToken; } + //static bool IsExpired(string token, string host) + //{ + // if (string.IsNullOrWhiteSpace(token)) + // { + // return true; + // } + // var jwtToken = new JwtSecurityToken(token); + // if (jwtToken.Audiences.First().Contains(host) == false) return true; + // return jwtToken.ValidTo < DateTime.UtcNow; + //} + #endregion #region Event Handlers for Controls on the Search Query Tab