Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading