[OOBE] Get "What's New" behind authenticated proxy and strict firewall. (#18695)

* Attempt to get username and password for proxy authentication. We should not use it.

* Using default credentials for system proxy authentication.
This commit is contained in:
lncubus
2022-06-14 12:44:07 +02:00
committed by GitHub
parent b5531a1f6b
commit e8bb2de8b6
3 changed files with 65 additions and 21 deletions

View File

@@ -6,6 +6,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
@@ -18,7 +19,6 @@ using Microsoft.PowerToys.Settings.UI.OOBE.Enums;
using Microsoft.PowerToys.Settings.UI.OOBE.ViewModel;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using Windows.UI.Core;
namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
{
@@ -55,14 +55,21 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
private static async Task<string> GetReleaseNotesMarkdown()
{
string releaseNotesJSON = string.Empty;
using (HttpClient getReleaseInfoClient = new HttpClient())
{
// GitHub APIs require sending an user agent
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required
getReleaseInfoClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "PowerToys");
releaseNotesJSON = await getReleaseInfoClient.GetStringAsync("https://api.github.com/repos/microsoft/PowerToys/releases");
}
// Let's use system proxy
using var proxyClientHandler = new HttpClientHandler
{
DefaultProxyCredentials = CredentialCache.DefaultCredentials,
Proxy = WebRequest.GetSystemWebProxy(),
PreAuthenticate = true,
};
using var getReleaseInfoClient = new HttpClient(proxyClientHandler);
// GitHub APIs require sending an user agent
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required
getReleaseInfoClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "PowerToys");
releaseNotesJSON = await getReleaseInfoClient.GetStringAsync("https://api.github.com/repos/microsoft/PowerToys/releases");
IList<PowerToysReleaseInfo> releases = JsonSerializer.Deserialize<IList<PowerToysReleaseInfo>>(releaseNotesJSON);
// Get the latest releases
@@ -83,23 +90,45 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Views
return releaseNotesHtmlBuilder.ToString();
}
private async void Page_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
private async Task Reload()
{
try
{
string releaseNotesMarkdown = await GetReleaseNotesMarkdown();
ProxyWarningInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
ErrorInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
ReleaseNotesMarkdown.Text = releaseNotesMarkdown;
ReleaseNotesMarkdown.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
LoadingProgressRing.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
}
catch (HttpRequestException httpEx)
{
Logger.LogError("Exception when loading the release notes", httpEx);
if (httpEx.Message.Contains("407", StringComparison.CurrentCulture))
{
ProxyWarningInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
}
else
{
ErrorInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
}
}
catch (Exception ex)
{
Logger.LogError("Exception when loading the release notes", ex);
LoadingProgressRing.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
ErrorInfoBar.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
}
finally
{
LoadingProgressRing.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
}
}
private async void Page_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
await Reload();
}
/// <inheritdoc/>