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
相关产品推荐
相关产品推荐

