Biraj Dahal
Biraj Dahal

Reputation: 57

How to pass the Client Id for google authetication in .Net Core 6

I am trying to use a google authentication in .Net Core 6. I have already generated the credentials in google developer console and added in the code.


using LoginKud.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(connectionString));

builder.Services.AddIdentity<IdentityUser, IdentityRole>()
        .AddEntityFrameworkStores<AppDbContext>();

builder.Services.AddAuthentication().AddGoogle(options => {
    options.ClientId = builder.Configuration["591241482908-66qgk38nbf1un6mxxxxxxxxxx.apps.googleusercontent.com"];
    options.ClientSecret = builder.Configuration["GOCSPX-jHKaxxxxxxx"];
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapRazorPages();

app.Run();


I have provided both client Id and Client Secret Id but I get The 'ClientId' option must be provided. (Parameter 'ClientId') error every time I run the program. How can I solve this?

Upvotes: 0

Views: 1925

Answers (1)

Serhii
Serhii

Reputation: 753

I thing the problem is inside this 2 lines:

   options.ClientId = builder.Configuration["591241482908-66qgk38nbf1un6mxxxxxxxxxx.apps.googleusercontent.com"];
options.ClientSecret = builder.Configuration["GOCSPX-jHKaxxxxxxx"];

You should pass there the key from your settings, not the value.

Try to pass values directly for now:

   options.ClientId = "591241482908 66qgk38nbf1un6mxxxxxxxxxx.apps.googleusercontent.com";
   options.ClientSecret = "GOCSPX-jHKaxxxxxxx";

And if it helps put in your configuration keys and values, like this:

"clientId":"591241482908 66qgk38nbf1un6mxxxxxxxxxx.apps.googleusercontent.com",
"clientSecret":"GOCSPX-jHKaxxxxxxx"

And rewrite your code to:

options.ClientId = builder.Configuration["clientId"];
options.ClientSecret = builder.Configuration["clientSecret"];

Upvotes: 2

Related Questions