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

fix: External Authentication in Middleware #1391

Closed
MikeRobGIT opened this issue Jan 27, 2024 · 2 comments · Fixed by #1410
Closed

fix: External Authentication in Middleware #1391

MikeRobGIT opened this issue Jan 27, 2024 · 2 comments · Fixed by #1410

Comments

@MikeRobGIT
Copy link

MikeRobGIT commented Jan 27, 2024

🐛 Bug Report

I've tried to add external authenticators to my app but the authenticator's schema name is being duplicated, even when specified.

💻 Repro or Code Sample

Update Program.cs to use external authentication:

var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;
// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents()
    .AddInteractiveWebAssemblyComponents();

builder.Services.AddFluentUIComponents();

builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<IdentityUserAccessor>();
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, PersistingRevalidatingAuthenticationStateProvider>();

builder.Services.AddAuthentication(options =>
    {
        options.DefaultScheme = IdentityConstants.ApplicationScheme;
        options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
    })
    .AddGoogle("Google", googleOptions =>
    {
        googleOptions.ClientId = config["Authentication:Google:ClientId"];
        googleOptions.ClientSecret = config["Authentication:Google:ClientSecret"];
        googleOptions.SignInScheme = IdentityConstants.ExternalScheme;
    })
    .AddIdentityCookies();

🤔 Expected Behavior

External authentication should work as it would using the default blazor template.

😯 Current Behavior

When the Google button is clicked I get the error below. The scheme is being recognized as "Google,Google" instead of "Google".

System.InvalidOperationException: No authentication handler is registered for the scheme 'Google,Google'. The registered schemes are: Google, Identity.Application, Identity.External, Identity.TwoFactorRememberMe, Identity.TwoFactorUserId. Did you forget to call AddAuthentication().AddSomeAuthHandler?
at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
at Microsoft.AspNetCore.Http.HttpResults.ChallengeHttpResult.ExecuteAsync(HttpContext httpContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteResultWriteResponse(IResult result, HttpContext httpContext)
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass104_2.<b__2>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Antiforgery.Internal.AntiforgeryMiddleware.InvokeAwaited(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

💁 Possible Solution

🔦 Context

I'm trying to allow external logins to make it easier for users to register.

🌍 Your Environment

  • Fluent Blazor Web App Template
  • Windows 11, PC
  • Browser: Google Chrome
  • .NET 8
@malthuswaswrong
Copy link

malthuswaswrong commented Jan 27, 2024

I just encountered the same issue. I confirmed it seems to be related to Fluent Blazor. A vanilla Blazor application authenticates fine.

malthuswaswrong added a commit to malthuswaswrong/fluentui-blazor that referenced this issue Jan 27, 2024
Fluent button has both "name" and "Value".  When the form is submitted these two values are concatenated together with a comma. 

Ex: "Google" becomes "Google,Google" and the login fails because there is no matching provider.

This was copied directly from the default Blazor template and the FluentButton seems to behave differently than a stock html button.
@malthuswaswrong
Copy link

The problem is the FluentButton provided by the Fluent new project template in ExternalLoginPicker.

<FluentButton Type="ButtonType.Submit" Appearance="Appearance.Accent" name="provider" Value="@provider.Name" Title="@($"Log in using your {provider.DisplayName} account")">@provider.DisplayName

If both "name" and "Value" are present it concatenates "Google,Google". If either one of them is removed, the provider comes back properly without the concatenation.

I tried to fix the template myself, but I am too stupid.

vnbaaij added a commit that referenced this issue Jan 30, 2024
@vnbaaij vnbaaij linked a pull request Jan 30, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants