Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Replace webrequest with HTTP client as it is obsolete #2352

Merged
merged 2 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,44 +754,38 @@ internal void InitializeTelemetry(ClientContext context, InitializationType init

internal static string GetRealmFromTargetUrl(Uri targetApplicationUri)
{
WebRequest request = WebRequest.Create(targetApplicationUri + "/_vti_bin/client.svc");
request.Headers.Add("Authorization: Bearer ");
var client = Framework.Http.PnPHttpClient.Instance.GetHttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "");

try
var response = client.GetAsync(targetApplicationUri + "/_vti_bin/client.svc").GetAwaiter().GetResult();
if (response == null)
{
using (request.GetResponse())
{
}
return null;
}
catch (WebException e)
{
if (e.Response == null)
{
return null;
}

string bearerResponseHeader = e.Response.Headers["WWW-Authenticate"];
if (string.IsNullOrEmpty(bearerResponseHeader))
{
return null;
}
var bearerResponseHeaderValues = response.Headers.GetValues("WWW-Authenticate");
string bearerResponseHeader = string.Join("", bearerResponseHeaderValues);

const string bearer = "Bearer realm=\"";
int bearerIndex = bearerResponseHeader.IndexOf(bearer, StringComparison.Ordinal);
if (bearerIndex < 0)
{
return null;
}
if (string.IsNullOrEmpty(bearerResponseHeader))
{
return null;
}

int realmIndex = bearerIndex + bearer.Length;
const string bearer = "Bearer realm=\"";
int bearerIndex = bearerResponseHeader.IndexOf(bearer, StringComparison.Ordinal);
if (bearerIndex < 0)
{
return null;
}

if (bearerResponseHeader.Length >= realmIndex + 36)
int realmIndex = bearerIndex + bearer.Length;
if (bearerResponseHeader.Length >= realmIndex + 36)
{
string targetRealm = bearerResponseHeader.Substring(realmIndex, 36);
if (Guid.TryParse(targetRealm, out _))
{
string targetRealm = bearerResponseHeader.Substring(realmIndex, 36);
if (Guid.TryParse(targetRealm, out _))
{
return targetRealm;
}
return targetRealm;
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/_debug/debug.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if ($PSEdition -eq 'Core') {
$netversion = "netcoreapp3.1"
}
else {
$netversion = "net461"
$netversion = "net462"
}

$BinPath = "$BinPath\$netversion"
Expand Down