Azure AD注销

问题描述 投票:3回答:2

我想从azure ad b2c中签出我的webapp。我尝试了以下https://www.janaks.com.np/azure-ad-identity-provider-in-aspnet-core-application/示例中的建议。

if (HttpContext.User.Identity.IsAuthenticated)
{
    await HttpContext.Authentication.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
    await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}

使用Startup.cs中的以下配置:

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
    AuthenticationScheme = settings.SignInPolicyId,
    AutomaticChallenge = true,
    CallbackPath = settings.SignInCallbackPath,
    ClientId = settings.ClientId,
    MetadataAddress = string.Format(settings.AadInstance, settings.Tenant, settings.SignInPolicyId),
    PostLogoutRedirectUri = settings.RedirectUri,
    TokenValidationParameters = new TokenValidationParameters
    {
        NameClaimType = "name"
    },
    AutomaticAuthenticate = true,
    Scope = { "openid" },
    ResponseType = "id_token",
    GetClaimsFromUserInfoEndpoint = true
});

但是,当我尝试从webapp注销后,将抛出异常:

InvalidOperationException: No authentication handler is configured to handle the scheme: OpenIdConnect

谢谢你的帮助。

azure logout azure-ad-b2c
2个回答
3
投票

您必须确定您设置的身份验证方案:

if (HttpContext.User.Identity.IsAuthenticated)
{
    await HttpContext.Authentication.SignOutAsync(settings.SignInPolicyId);
    await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}

您将以某种方式获得该控制器的策略ID并使用它来识别适当的中间件。


1
投票

接受的答案适用于Auth 1,但在Auth 2中该方法已折旧,因此请使用扩展方法。

await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

参考:https://github.com/aspnet/Announcements/issues/232

© www.soinside.com 2019 - 2024. All rights reserved.