Skip to content

Commit

Permalink
Feature: Replace webrequest with HTTP client as it is obsolete (#2352)
Browse files Browse the repository at this point in the history
* Replace usage of webrequest with http client

* improve debug script
  • Loading branch information
gautamdsheth authored Sep 17, 2022
1 parent 5822e83 commit ab3d553
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
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

0 comments on commit ab3d553

Please sign in to comment.