Skip to content

Commit

Permalink
Added input field for optional tenant id
Browse files Browse the repository at this point in the history
  • Loading branch information
wobba committed Apr 18, 2021
1 parent bcbb050 commit ecb7e3a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>

<DockPanel HorizontalAlignment="Stretch">
Expand All @@ -1135,26 +1136,40 @@
LostFocus="SharePointSiteUrlTextBox_LostFocus"
TextAlignment="Left" />
</DockPanel>

<StackPanel Grid.Row="1" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">
<Label Content="Tenant Id (optional):"/>
<TextBox x:Name="TenantIdTextBox" Height="26"
Margin="5,0,5,1"
Text=""
Padding="4"
TextAlignment="Left" Width="230">
<TextBox.ToolTip>
<TextBlock><Run Text="Needed for non sharepoint.com domains (contoso.com or guid)."/></TextBlock>
</TextBox.ToolTip>
</TextBox>
</StackPanel>

<StackPanel Grid.Row="3" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">
<Label Content="Timeout (sec):"/>
<TextBox x:Name="WebRequestTimeoutTextBox" Height="26"
Margin="5,0,5,1"
Text="30"
Padding="4"
TextAlignment="Left" Width="50" RenderTransformOrigin="1.847,0.53"/>
TextAlignment="Left" Width="50"/>
</StackPanel>
<StackPanel Grid.Row="2" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">
<StackPanel Grid.Row="4" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">
<Label Content="Accept:"/>
<RadioButton x:Name="AcceptJsonRadioButton" Content="json" IsChecked="True" VerticalAlignment="Center" GroupName="AcceptFormat" />
<Rectangle Width="10"/>
<RadioButton x:Name="AcceptXmlRadioButton" Content="xml" VerticalAlignment="Center" GroupName="AcceptFormat" />
</StackPanel>
<DockPanel Grid.Row="3" Margin="0,10,0,0">
<DockPanel Grid.Row="5" Margin="0,10,0,0">
<Label Content="Method:" />
<RadioButton VerticalContentAlignment="Center" IsChecked="True" Checked="HttpMethodModeRadioButton_Checked" x:Name="HttpGetMethodRadioButton" GroupName="HttpMethodMode" Content="HTTP GET"/>
<RadioButton Margin="5,0,0,0" VerticalContentAlignment="Center" Checked="HttpMethodModeRadioButton_Checked" x:Name="HttpPostMethodRadioButton" GroupName="HttpMethodMode" Content="HTTP POST"/>
</DockPanel>
<StackPanel Grid.Row="4" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">
<StackPanel Grid.Row="6" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
Expand Down Expand Up @@ -1220,15 +1235,9 @@
<Label x:Name="LoggedinLabel" Padding="5" Visibility="Hidden" Content="Successfully signed-in" FontWeight="Bold" Foreground="#FF275706"/>
</StackPanel>
</Grid>



</Grid>



</StackPanel>
<StackPanel Grid.Row="5" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal"/>
<StackPanel Grid.Row="7" Margin="0,10,0,0" HorizontalAlignment="Stretch" Orientation="Horizontal"/>
</Grid>
</Expander.Content>
</Expander>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ecb7e3a

Please sign in to comment.