I had follow this tutorial for setup External Login https://code-maze.com/external-identity-provider-aspnet-core-identity/, after when I want to test login with google account, I write my email, my password. I get to fuunction ExternalLoginConfirmation where ModelState is not valid, always because Principal is null and if I do not specify returnUrl also because of that.
Here are pictures of Debug:


Now I'm not sure why returnUrl must be mandatory field, this can easy manage if null is to have to return to Home/Index.

Then when I submit I got ModelStata.IsValid as false

From Program.cs I have this configuration:
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder =>
{
containerBuilder.RegisterModule(new DefaultDomainModule());
containerBuilder.RegisterModule(new DefaultInfrastructureModule(false));
});
builder.Services.AddAuthentication()
.AddGoogle(options =>
{
IConfigurationSection googleAuthSection = builder.Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthSection["ClientId"];
options.ClientSecret = googleAuthSection["ClientSecret"];
options.SaveTokens = true;
});
And Code from InfrastructureLayer:
public static void AddIdentityDbContext(this IServiceCollection services, string connectionString) =>
services.AddDbContext<IdentityAppDbContext>(options =>
{
options.UseSqlServer(connectionString);
})
.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequiredLength = 4;
options.SignIn.RequireConfirmedEmail = true;
options.Lockout.MaxFailedAccessAttempts = 5;
options.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<IdentityAppDbContext>()
.AddDefaultTokenProviders();
ExternalLoginCallBack returnUrl parameter set to nullable to solves this issue:
from
public async Task ExternalLoginCallBack(string returnUrl = null)
to
public async Task ExternalLoginCallBack(string? returnUrl = null)
https://github.com/dotnet/aspnetcore/issues/22656#issuecomment-640650945
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With