You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP Identity API登出报错:未注册Identity.Application登出处理程序

ASP.NET Identity API登出报错解决方案

错误原因

报错提示明确说明:SignInManager.SignOutAsync() 默认尝试使用 Identity.Application 认证方案,但你当前仅注册了 Identity.Bearer 方案的登出处理器,未为 Identity.Application 配置对应的Cookie认证(纯API场景通常不需要Cookie)。


方案1:API场景下的正确登出方式(推荐)

Bearer Token是无状态认证,服务器端无需执行登出操作,让客户端直接丢弃本地存储的Token即可。你可以简化登出接口:

app.MapGroup("/account").MapPost("/logout", () =>
{
    return Results.Ok(new { message = "登出成功,请客户端丢弃Token" });
})
.WithOpenApi()
.RequireAuthorization();

方案2:指定Bearer方案调用SignOutAsync

如果你一定要使用SignInManager.SignOutAsync(),需显式指定使用IdentityConstants.BearerScheme,避免默认使用Cookie方案:

app.MapGroup("/account").MapPost("/logout", async (SignInManager<AgentPortalUser> signInManager,
    [FromBody] object empty) =>
{
    if (empty != null)
    {
        // 显式指定Bearer方案
        await signInManager.SignOutAsync(IdentityConstants.BearerScheme);
        return Results.Ok();
    }
    return Results.Unauthorized();
})
.WithOpenApi()
.RequireAuthorization();

也可以全局配置Identity默认登出方案:

builder.Services.ConfigureApplicationCookie(options =>
{
    options.SignOutScheme = IdentityConstants.BearerScheme;
});

注意:该配置对纯API场景意义有限,因为Bearer Token无需服务器端销毁。


内容的提问来源于stack exchange,提问作者karthik suryadevara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 22:52:20