Setting up Google Sign in with Blazor WebAssembly and WebApi app

Viewed 463

I am trying to create a Blazor WebAssembly App that has Google Sign in and can then call server WebAPI project and somehow pass the info about logged in user. Both blazor client and API server only needs to get user's email in a verifiable manner. I don't need to register users in advance or call any third party APIs.

I managed to set up the client side for logging in and for passing some token in a Bearer http header using this on Blazor WebAssembly side:

builder.Services
    .AddHttpClient("WebApi", client =>
    {
        client.BaseAddress = new Uri(builder.Configuration["ServerAPI:ApplicationUrl"]!);
    })
    .AddHttpMessageHandler(sp =>
    {
        var handler = sp.GetRequiredService<AuthorizationMessageHandler>()
            .ConfigureHandler(
                authorizedUrls: new[] {builder.Configuration["ServerAPI:ApplicationUrl"]!}
            );
        return handler;
    });

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("WebApi"));

and this appsettings.json:

{
    "Local": {
        "Authority": "https://accounts.google.com/",
        "ClientId": "_________secret___________.apps.googleusercontent.com",
        "DefaultScopes": [ "email" ],
        "PostLogoutRedirectUri": "https://localhost:7210/authentication/logout-callback",
        "RedirectUri": "https://localhost:7210/authentication/login-callback",
        "ResponseType": "id_token token"
    },
    "ServerAPI": {
        "ApplicationUrl": "https://localhost:7250"
    }
}

Now when I call the WebAPI I can see that Http header Bearer is set to some string of characters but no matter how I try to setup WebAPI project it fails to authenticate the user.

So on WebAPI side with what options should I call AddAuthentication, should I call AddGoogle, AddJwtBearer or something else and how? I tried variations on these but nothing seems to work.

1 Answers

It seems you can't really accomplish what I wanted with the access token you get from Google but you can get something with a bit more complicated setup. It is best explained in this article.

Related