外部提供商登录 .NET
External Provider Login .NET
我已经按照本教程设置了外部登录 https://code-maze.com/external-identity-provider-aspnet-core-identity/,当我想用 google 帐户测试登录后,我写了我的电子邮件和密码。我得到 ExternalLoginConfirmation 函数,其中 ModelState 无效,总是因为 Principal 为空,如果我没有指定 returnUrl 也是因为这个。
下面是Debug的图片:
现在我不确定为什么 returnUrl 必须是必填字段,如果 null 必须 return 到 Home/Index,这很容易管理。
然后当我提交时我得到 ModelStata.IsValid 为 false
来自Program.cs 我有这样的配置:
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;
});
基础设施层的代码:
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 参数设置为 nullable 以解决此问题:
来自
public async Task ExternalLoginCallBack(string returnUrl = null)
到
public async Task ExternalLoginCallBack(string? returnUrl = null)
https://github.com/dotnet/aspnetcore/issues/22656#issuecomment-640650945
我已经按照本教程设置了外部登录 https://code-maze.com/external-identity-provider-aspnet-core-identity/,当我想用 google 帐户测试登录后,我写了我的电子邮件和密码。我得到 ExternalLoginConfirmation 函数,其中 ModelState 无效,总是因为 Principal 为空,如果我没有指定 returnUrl 也是因为这个。
下面是Debug的图片:
现在我不确定为什么 returnUrl 必须是必填字段,如果 null 必须 return 到 Home/Index,这很容易管理。
然后当我提交时我得到 ModelStata.IsValid 为 false
来自Program.cs 我有这样的配置:
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;
});
基础设施层的代码:
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 参数设置为 nullable 以解决此问题:
来自
public async Task ExternalLoginCallBack(string returnUrl = null)
到
public async Task ExternalLoginCallBack(string? returnUrl = null)
https://github.com/dotnet/aspnetcore/issues/22656#issuecomment-640650945