Slava
Slava

Reputation: 6650

What the proper way to implement Google Open ID Connect with .NET Core 2.1?

I am trying to learn ASP.NET Core Authentication options by following Pluralsight training. In that training they use Azure for Authentication.

I want to use Google. Here is the code to add Google Auth:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddOpenIdConnect(options =>
        {
            _configuration.Bind("Google", options);
        })
        .AddCookie();

        services.AddSingleton<IGreeter, Greeter>(); // Dependency Injection for custom service Greeter
        services.AddDbContext<OdeToFoodDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("OdeToFood")));
        services.AddScoped<IRestaurantData, SqlRestaurantData>(); // scoped to http transaction, dbcontext is not thread safe
        services.AddMvc();
    }

In appsettings.json I have these defined:

{
    "Google": {
        "ClientId": "234092845903-n92krp955lrp46mdf445g5vo0sqp2eks.apps.googleusercontent.com",
        "ClientSecret": "bRg1flFud87hfsef89jMKoGW"
    },
    "Greeting":  "Hello from appsettings.json !!",
    "ConnectionStrings": {
        "OdeToFood": "Server=(localdb)\\MSSQLLocalDB;Database=OdeToFood;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
}

However, when I run the app, instead of Google Sign In screen I get an error:

An unhandled exception occurred while processing the request. InvalidOperationException: Provide Authority, MetadataAddress, Configuration, or ConfigurationManager to OpenIdConnectOptions Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions.Validate()


InvalidOperationException: Provide Authority, MetadataAddress, Configuration, or ConfigurationManager to OpenIdConnectOptions Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectOptions.Validate() Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions.Validate(string scheme) Microsoft.AspNetCore.Authentication.AuthenticationHandler.InitializeAsync(AuthenticationScheme scheme, HttpContext context) Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, string authenticationScheme) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

What am I doing wrong?

ClientID and ClientSecret are defined in Google Developers Console.

Upvotes: 1

Views: 997

Answers (1)

Llazar
Llazar

Reputation: 3312

Add this method in Startup class.

    services.AddAuthentication().AddGoogle(googleOptions =>
    {
    googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
    googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
    }); 

Upvotes: 1

Related Questions