I want to implement Google authentication with the authorization code flow in Blazor WASM, but there isn’t much information on the internet and I am quite confused.
I was implementing Google authentication by referring to the following site.
https://www.telerik.com/blogs/create-webassembly-app-blazor-google-authorization
I successfully implemented using response_type="id_token"
. However, when I switched the response_type
to "code"
, I could sign in to Google, but the login failed (there was an error trying to log you in: ‘There was an error signing in.’)
program.cs
:
builder.Services.AddOidcAuthentication(options =>
{
options.ProviderOptions.Authority = "https://accounts.google.com/";
options.ProviderOptions.ClientId = "~~client id~~";
options.ProviderOptions.RedirectUri = "https://localhost:0000/authentication/login-callback";
options.ProviderOptions.ResponseType = "code";
options.ProviderOptions.DefaultScopes.Add("openid");
options.ProviderOptions.DefaultScopes.Add("profile");
});
Authentication.razor
:
@page "/authentication/{action}"
<RemoteAuthenticatorView Action="@Action" OnLogInSucceeded="OnLogInSucceeded"/>
@code {
[Parameter] public string? Action { get; set; }
private async Task OnLogInSucceeded()
{
~~send the authorization code to the server and receive the access token.~~
}
}
Login.razor
:
@page "/auth/login"
<h3>Log in</h3>
<button @onclick="@(()=>NavManager.NavigateToLogin("authentication/login"))">log in with google</button>
I manually create the authentication request and use
NavigationManager.NavigateTo("https://accounts.google.com/o/oauth2/v2/auth?~~~")
to redirect to the Google authentication page, the login succeeds and I am able to receive the authorization code.
Am I missing something, or is it not possible to use
<RemoteAuthenticatorView Action="@Action">
when implementing Google authentication with the authorization code flow?
Kazuki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.