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

Attempted Use With Blazor wasm return "Property AllowAutoRedirect is not supported." #2188

Closed
microhobby opened this issue May 12, 2020 · 13 comments
Labels
Status: Stale Used by stalebot to clean house Type: Bug Something isn't working as documented

Comments

@microhobby
Copy link

I am trying to use the library with Blazor wasm template but this returns the stack:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Property AllowAutoRedirect is not supported.
System.PlatformNotSupportedException: Property AllowAutoRedirect is not supported.
  at System.Net.Http.HttpClientHandler.set_AllowAutoRedirect (System.Boolean value) <0x2149488 + 0x0000c> in <filename unknown>:0 
  at Octokit.Internal.HttpMessageHandlerFactory.CreateDefault (System.Net.IWebProxy proxy) <0x212fa80 + 0x00012> in <filename unknown>:0 
  at Octokit.Internal.HttpMessageHandlerFactory.CreateDefault () <0x212f5f8 + 0x00002> in <filename unknown>:0 
  at Octokit.Internal.HttpClientAdapter..ctor (System.Func`1[TResult] getHandler) <0x212f248 + 0x00020> in <filename unknown>:0 
  at Octokit.Connection..ctor (Octokit.ProductHeaderValue productInformation, System.Uri baseAddress, Octokit.ICredentialStore credentialStore) <0x212eb70 + 0x0001c> in <filename unknown>:0 
  at Octokit.Connection..ctor (Octokit.ProductHeaderValue productInformation, System.Uri baseAddress) <0x2129d58 + 0x00010> in <filename unknown>:0 
  at Octokit.GitHubClient..ctor (Octokit.ProductHeaderValue productInformation) <0x2121108 + 0x0000c> in <filename unknown>:0 
  at microhobby.Pages.Index.OnAfterRenderAsync (System.Boolean firstRender) <0x20f4e28 + 0x00034> in <filename unknown>:0 

I know this must be a limitation of Blazor wasm. But I'm curious if anyone has the same problem and if there is any workaround. Or if we should open an issue in the dotnet project (I really don't know why AllowRedirect is not supported by Blazor wasm)

@shiftkey
Copy link
Member

Or if we should open an issue in the dotnet project (I really don't know why AllowRedirect is not supported by Blazor wasm)

@microhobby let's start from there, as I don't have much control over HttpClientHandler and how it can be used from Blazor.

The relevant code is here:

var handler = new HttpClientHandler
{
AllowAutoRedirect = false
};

@HasinduLanka
Copy link

Exactly same bug here :-(

@HasinduLanka
Copy link

It works on .net core
But .net standard doesn't have SslProtocols
Please help!

@shiftkey
Copy link
Member

shiftkey commented Jun 5, 2020

@HasinduLanka I need more information to understand the problem you're seeing

@HasinduLanka
Copy link

When you try to authenticate Octokit in blazor, which runs on .net standard framework, you get the above error
This page gives an answer
https://stackoverflow.com/questions/46400797/httpclienthandler-throwing-platformnotsupportedexception

It seems aws are also getting the same problem
aws/aws-sdk-net#1307

@shiftkey
Copy link
Member

shiftkey commented Jun 5, 2020

@HasinduLanka the only area I'm aware of where we enable these in the library is here:

#if HAS_SERVICEPOINTMANAGER
// GitHub API requires TLS1.2 as of February 2018
//
// .NET Framework before 4.6 did not enable TLS1.2 by default
//
// Even though this is an AppDomain wide setting, the decision was made for Octokit to
// ensure that TLS1.2 is enabled so that existing applications using Octokit did not need to
// make changes outside Octokit to continue to work with GitHub API
//
// *Update*
// .NET Framework 4.7 introduced a new value (SecurityProtocolType.SystemDefault = 0)
// which defers enabled protocols to operating system defaults
// If this is the current value we shouldn't do anything, as that would cause TLS1.2 to be the ONLY enabled protocol!
//
// See https://docs.microsoft.com/en-us/dotnet/api/system.net.securityprotocoltype?view=netframework-4.7
// See https://github.com/octokit/octokit.net/issues/1914
// Only apply when current setting is not SystemDefault (0) added in .NET 4.7
if ((int)ServicePointManager.SecurityProtocol != 0)
{
// Add Tls1.2 to the existing enabled protocols
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
}
#endif

This is only enabled for net46:

<PropertyGroup Condition=" '$(TargetFramework)' == 'net46' ">
<DefineConstants>$(DefineConstants);HAS_ENVIRONMENT;HAS_REGEX_COMPILED_OPTIONS;SIMPLE_JSON_INTERNAL;SIMPLE_JSON_OBJARRAYINTERNAL;SIMPLE_JSON_READONLY_COLLECTIONS;HAS_SERVICEPOINTMANAGER</DefineConstants>
</PropertyGroup>

This shouldn't be enabled for .NET Standard as we don't include that ifdef.

@HasinduLanka
Copy link

I was playing with this error for few hours.

Things I saw,

  • Create .net standard 2.0 project, refer octokit.net nuget and consume it -> WORKS

  • Create .net standard 2.1 project (like Blazor wasm), refer octokit.net nuget, consume it with a .net core 3.1 app -> WORKS

  • Create .net standard 2.1 project (like Blazor wasm), refer octokit.net nuget, consume it -> ERROR
    Property AllowAutoRedirect is not supported. System.PlatformNotSupportedException: Property AllowAutoRedirect is not supported. at System.Net.Http.HttpClientHandler.set_AllowAutoRedirect (System.Boolean value) <0x2149488 + 0x0000c> in <filename unknown>:0

  • Clone this repo, Remove AllowAutoRedirect = false from HttpMessageHandlerFactory.cs, Consume it with a .net standard 2.1 project (like Blazor wasm) -> Above error is gone, But It does not really Authenticate GitHubClient on
    Client = new GitHubClient(new ProductHeaderValue("Product Name")) { Credentials = new Credentials(username, password) };

So I get
Octokit.AuthorizationException: Bad Credentials at Octokit.Connection.HandleErrors (Octokit.IResponse response) [0x00018] in <PATH>\octokit.net\Octokit\Http\Connection.cs:686

It's framework issues, I'm very thankful for helping us on these :-)

@HasinduLanka
Copy link

HasinduLanka commented Jun 6, 2020

It looks like Blazor Web Assembly doesn't have AllowAutoRedirect property because HttpClientHandler is implemented via JavaScript APIs.
There is an issue with SecurityProtocolType.Tls12

This guy has Modified octokit.net to use with blazor

He changed
_http = new HttpClient(new RedirectHandler { InnerHandler = getHandler() });
to
_http = new HttpClient();

of Http/HttpClientAdapter.cs

When I do that it still says Octokit.AuthorizationException: Bad Credentials at Octokit.Connection.HandleErrors

Exactly same code runs well on .net core

HasinduLanka referenced this issue in martinmthomas/octokit.net Jun 6, 2020
… that this can be used in .net core 3.0 and blazor.
@martinmthomas
Copy link

@HasinduLanka, I agree with your observation on why AutoRedirect does not work in Blazor. Also see #16955. Though this is marked as closed, you can see that there are still open comments about it. To workaround this, the best way is to remove the AutoRedirect property in the HttpMessageHandlerFactory.cs file and not setting it to True. For whatever reason, Blazor does not like we setting the value (even if it is the default value) to this property. Also, you can remove the default handler completely when creating HttpClient instance but this prevents additional code in the default handler which might work.

I assume there must be a reason why Octokit prevents AutoRedirect in the implementation. It is better if someone can explain that so that you are not creating any other issue down the line by enabling it.

As with the auth issue, I suppose it is because Basic Auth is deprecated. I have tried with my Personal Access Token and it is working for me. I used below syntax

var github = new GitHubClient(new ProductHeaderValue("Product Name"))
        {
            Credentials = new Credentials("Provide_PersonalAccessToken_Here", AuthenticationType.Bearer)
        };

It might be worth retrying with a PAT after 2FA is enabled just to see if it is an issue with Basic Auth.

@shiftkey
Copy link
Member

shiftkey commented Jun 6, 2020

I assume there must be a reason why Octokit prevents AutoRedirect in the implementation. It is better if someone can explain that so that you are not creating any other issue down the line by enabling it.

@martinmthomas from memory this is still kept around due to behaviour around edge cases that have built up over the years:

if (response.StatusCode == HttpStatusCode.MovedPermanently
|| response.StatusCode == HttpStatusCode.Redirect
|| response.StatusCode == HttpStatusCode.Found
|| response.StatusCode == HttpStatusCode.SeeOther
|| response.StatusCode == HttpStatusCode.TemporaryRedirect
|| (int)response.StatusCode == 308)
{
if (response.StatusCode == HttpStatusCode.SeeOther)
{
clonedRequest.Content = null;
clonedRequest.Method = HttpMethod.Get;
}
// Increment the redirect count
clonedRequest.Properties[RedirectCountKey] = ++redirectCount;
// Set the new Uri based on location header
clonedRequest.RequestUri = response.Headers.Location;
// Clear authentication if redirected to a different host
if (string.Compare(clonedRequest.RequestUri.Host, request.RequestUri.Host, StringComparison.OrdinalIgnoreCase) != 0)
{
clonedRequest.Headers.Authorization = null;
}
// Send redirected request
response = await SendAsync(clonedRequest, cancellationToken).ConfigureAwait(false);
}

The blame for this file has more context and links to the relevant commits, and I think there were cases around us having to explicitly handle things better than the defaults on Windows (where this library originated) that may no longer be necessary.

@HasinduLanka
Copy link

Everyone, thank you very much - Issue is fixed!

Just like @shiftkey said I removed the line AllowAutoRedirect = false from HttpMessageHandlerFactory.cs

Then octokit didn't authenticated because, HTTPClient get redirected and doesn't return the correct result back when using password authentication.

But just like @martinmthomas said, this doesn't occur in Personal Token Based Auth. So using token instead of password allowed me to Authenticate without any error.

p.s. I'll make this repo public as soon as our 'Account servers' are ready. So anyone could cope with this

Again thanks for quick help!

@johan-v-r
Copy link

Seems very much related to this dotnet/runtime issue: [wasm] Blazor 303 responses, and fetch vs navigation #39365

@github-actions
Copy link

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

@github-actions github-actions bot added the Stale label May 13, 2022
@nickfloyd nickfloyd added Type: Bug Something isn't working as documented Status: Stale Used by stalebot to clean house and removed category: bug labels Oct 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Used by stalebot to clean house Type: Bug Something isn't working as documented
Projects
None yet
Development

No branches or pull requests

7 participants
@nickfloyd @shiftkey @martinmthomas @microhobby @johan-v-r @HasinduLanka and others