Asp.net core mvc MVC核心中的IdentityServer登录错误:加载外部登录信息时出错
环境:MVC Core 3.1、VS2019、IdentityServer 4 我使用“个人用户帐户”在VS2019中创建了asp.net核心web应用程序。将其修改为使用IdentityServer对用户进行身份验证(我们有一个使用IdentityServer 4构建的身份验证服务)。当我通过单击“OpenIdConnect”按钮登录时,我被正确重定向到IdentityServer,在那里我输入了用户名和密码,并成功通过了身份验证(我可以从IdentityServer的日志中验证这一点)。但当重定向回我的应用程序时,我遇到了这个错误“加载外部登录信息时出错” IdentityServer不应该是问题所在,因为我们有其他使用Web表单构建的应用程序,并且MVC成功地对其进行了身份验证。但这是我们第一个使用IdentityServer的MVC核心项目,所以我不知道我是否遗漏了一些选项 我搜索了所有的互联网,但没有找到解决方案。请帮忙 我在Startup.cs中的代码如下所示:Asp.net core mvc MVC核心中的IdentityServer登录错误:加载外部登录信息时出错,asp.net-core-mvc,visual-studio-2019,identityserver4,Asp.net Core Mvc,Visual Studio 2019,Identityserver4,环境:MVC Core 3.1、VS2019、IdentityServer 4 我使用“个人用户帐户”在VS2019中创建了asp.net核心web应用程序。将其修改为使用IdentityServer对用户进行身份验证(我们有一个使用IdentityServer 4构建的身份验证服务)。当我通过单击“OpenIdConnect”按钮登录时,我被正确重定向到IdentityServer,在那里我输入了用户名和密码,并成功通过了身份验证(我可以从IdentityServer的日志中验证这一点)。但当
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "[my-identityserver-url]";
options.RequireHttpsMetadata = true;
options.ClientId = "[my-client-id]";
options.ClientSecret = "[my-client-secret]";
options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
options.UsePkce = true;
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = JwtClaimTypes.Name,
RoleClaimType = JwtClaimTypes.Role,
};
});
}
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
services.AddDefaultIdentity(options=>options.SignIn.RequireConfirmedAccount=true)
.AddEntityFrameworkStores();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddAuthentication(选项=>
{
options.DefaultScheme=“Cookies”;
options.DefaultChallengeScheme=“oidc”;
})
.AddCookie(“Cookies”)
.AddOpenIdConnect(“oidc”,选项=>
{
options.signnscheme=“Cookies”;
options.Authority=“[my identityserver url]”;
options.RequireHttpsMetadata=true;
options.ClientId=“[my client id]”;
options.ClientSecret=“[my client secret]”;
options.ResponseType=OpenIdConnectResponseType.CodeIdToken;
options.UsePkce=true;
options.SaveTokens=true;
options.GetClaimsFromUserInfoEndpoint=true;
options.Scope.Clear();
options.Scope.Add(“openid”);
选项。范围。添加(“配置文件”);
options.GetClaimsFromUserInfoEndpoint=true;
options.SaveTokens=true;
options.TokenValidationParameters=新的TokenValidationParameters
{
NameClaimType=JwtClaimTypes.Name,
RoleClaimType=JwtClaimTypes.Role,
};
});
}
IdentityServer上的设置-
登录重定向URI:https://localhost:44313/signin-oidc
注销后重定向URI:https://localhost:44313/
作用域:openid,profile
授予类型:混合、客户端凭据
CORS来源:https://localhost:44313/
https://localhost:44313/ 是web应用程序的url。在我在.Net Core中完成的所有MVC客户端IS4实现中,我都使用了响应类型“Code”(并且仅使用Code)。我相信只有当ResponseType设置为“Code”时,PCKE才能工作。尝试更改您的ResponseType.OpenIdConnectResponseType.CodeIdToken==“代码id\U令牌”。当我只在那里输入“code”时,我得到了一个错误:“对不起,有一个错误:unauthorized\u client”您的评论有点混乱。我建议的行应该是“options.ResponseType=OpenIdConnectResponseType.code;“这就是您尝试的吗?另外,在IdentityServer4端,MVC客户端的“AllowedGrantTypes”选项是什么?(在Config.cs中)谢谢。是的,我尝试了options.ResponseType=“code”,但出现了错误。我在IdentityServer4端没有看到“AllowedGrantTypes”选项。我将发布该代码。”。