Azure AD B2C调用signin oidc回调时出现500错误

Azure AD B2C调用signin oidc回调时出现500错误,azure,authentication,asp.net-core,azure-ad-b2c,Azure,Authentication,Asp.net Core,Azure Ad B2c,我正在编写一个ASP.NETCore2.0Web应用程序,并尝试使用Azure AD B2C进行身份验证 public void配置服务(IServiceCollection服务) { services.AddDbContext(选项=> options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”)); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.C

我正在编写一个ASP.NETCore2.0Web应用程序,并尝试使用Azure AD B2C进行身份验证

public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(选项=>
{
options.DefaultScheme=JwtBearerDefaults.AuthenticationScheme;
options.defaultsignnscheme=JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
})
.AddJwtBearer(选项=>
{
选项。权限=”https://login.microsoftonline.com";
选项。观众=“澳元”;
options.Events=newjwtbearerevents
{
OnAuthenticationFailed=t=>
{
返回Task.FromResult(0);
}
};
})
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme,选项=>
{
options.MetadataAddress=$”https://login.microsoftonline.com/{Configuration[“AzureAdB2C:Tenant”]}/v2.0/。众所周知的/openid配置?p={Configuration[“AzureAdB2C:Policy”]};
options.ClientId=配置[“AzureAdB2C:ClientId”];
options.Events=newOpenIDConnectEvents{OnAuthenticationFailed=AuthenticationFailed,OnTokenValidated=Validated,OnRemoteFaile=Failed};
options.SaveTokens=true;
});
services.AddCors(o=>o.AddPolicy(“MyPolicy”,builder=>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}));
services.AddMvc();
配置(选项=>
{
options.Filters.Add(新的CorsAuthorizationFilterFactory(“MyPolicy”);
});
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseAuthentication();
if(env.IsDevelopment())
{
var builder=new ConfigurationBuilder();
builder.AddUserSecrets();
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(新的WebpackDevMiddlewareOptions
{
HotModuleReplacement=true,
ReactHotModuleReplacement=true
});
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
附录UseCors(“我的政策”);
app.UseMvc();
}
我在控制器操作上有一个
[Authorize]
属性,当我点击链接时,我会看到Microsoft登录页面。我成功登录,它将我重定向回
https://localhost:[PORT]/signin oidc
,返回500服务器错误


我的问题是有人知道为什么会这样吗?我想这可能与CORS有关,但看起来不像。来自Microsoft的帖子似乎包含有效的令牌。

Visual Studio调试窗口通常有一些有用的信息来帮助解决问题


我将更改您的启动代码以匹配(删除对
AddOpenIdConnect()
的调用):

services.AddAuthentication(选项=>
{
options.DefaultScheme=JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions=>
{
jwtOptions.Authority=$”https://login.microsoftonline.com/tfp/{Configuration[“AzureAdB2C:Tenant”]}/{Configuration[“AzureAdB2C:Policy”]}/v2.0/”;
jwtOptions.accession=Configuration[“AzureAdB2C:ClientId”];
事件=新的JWTBeareEvents
{
OnAuthenticationFailed=身份验证失败
};
});


我还将在
JwtBearerEvents
中的
OnMessageReceived
中添加一个处理程序。在该处理程序上设置一个断点,然后查看是否达到了预期效果。

您需要进一步调试该问题。500个错误可能意味着什么。虽然顺便说一下,您的JWT承载身份验证配置错误。权限未指向正确的权限,访问群体应为客户端id/App id URI。如果在AuthenticationFailed上添加断点,是否会命中?如果是这样,请检查
t
并查看它是否有更多详细信息。我在OnAuthenticationFailed上添加了断点,但没有命中。关于我还可以做些什么来调试,有什么建议吗?我在发布到
/oidc signin
时遇到了与使用令牌从AD返回时“相同”的问题。在我的例子中,有趣的是,只有在QA环境中部署时才会发生这种情况,而不是在开发环境中,也不是在本地环境中。我看到一个javascript错误
未声明纯文本文档的字符编码
。你成功地调试了这个问题吗?您使用哪个
OpenIdConnectEvents
的事件访问问题?