例外:关联失败。未知位置(Azure AD B2C)

时间:2020-07-16 18:37:48

标签: c# asp.net-core openid-connect azure-ad-b2c

我有一个具有以下配置的ASP.NET Core 2.2应用程序:

enter image description here

这是我的Startup.cs类

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = "Cookies";
        options.DefaultChallengeScheme = "oidc";
    })
    .AddCookie("Cookies")
    .AddOpenIdConnect("oidc", options =>
    {
        options.SignInScheme = "Cookies";
        options.Authority = $"{ Configuration.GetValue<string>("AzureAdB2C:Instance") }/{ Configuration.GetValue<string>("AzureAdB2C:Tenant")  }/{ Configuration.GetValue<string>("AzureAdB2C:SignUpSignInPolicyId") }/v2.0";
        options.ClientId = Configuration.GetValue<string>("AzureAdB2C:ClientId");
        options.ResponseType = "code id_token";
        options.SaveTokens = true;
        options.ClientSecret = Configuration.GetValue<string>("AzureAdB2C:ClientSecret");
    });

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

}

运行此命令时,会弹出B2C登录页面,但是一旦输入凭据,就会出现以下错误。 URL重定向到https:// localhost:xxxx / signin-oidc

enter image description here

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这类似于issue,在取消外部登录并重定向到源应用程序时发生了相同的错误。您可以使用OnRemoteFailure来处理异常。

AddOpenIdConnect("oidc", options => {
    // ...
    options.Events = new OpenIdConnectEvents
    {
        OnRemoteFailure = ctx =>
        {
            ctx.Response.Redirect("/error?FailureMessage=" + UrlEncoder.Default.Encode(ctx.Failure.Message)); 
            ctx.HandleResponse(); 
            return Task.FromResult(0); 
        }
    };  
});