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

IdentityServer自定义AccountController始终返回404 NotFound求助

Identity Server自定义AccountController接口返回404问题排查

问题场景

在Identity Server中创建了自定义AccountController,配置了LocalApi相关资源、客户端权限,但调用https://localhost:5001/api/account/signup始终返回404 NotFound。

已实现的配置代码

自定义AccountController代码

[Route("api/[controller]")]
[ApiController]
[Authorize(IdentityServerConstants.LocalApi.PolicyName)]
public class AccountController : ControllerBase
{
    public AccountController()
    {
        
    }

    [HttpGet("Signup")]
    public IActionResult Signup()
    {
        return Ok();
    }
}

API资源配置

public static IEnumerable<ApiResource> ApiResources => new List<ApiResource> {
        new ApiResource(IdentityServerConstants.LocalApi.ScopeName)
    };

IdentityServer服务配置

builder.Services.AddIdentityServer(options =>
            {
                options.EmitStaticAudienceClaim = true;
            })
            .AddInMemoryIdentityResources(Config.IdentityResources)
            .AddInMemoryApiScopes(Config.ApiScopes)
            .AddInMemoryClients(Config.Clients)
            .AddInMemoryApiResources(Config.ApiResources);

Program.cs添加的服务

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddLocalApiAuthentication();

客户端配置

new Client
{
    ClientId = "client.web",

    AccessTokenType = AccessTokenType.Jwt,
    IncludeJwtId = true,

    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

    ClientSecrets =
    {
        new Secret("secret12345".Sha256())
    },
    RedirectUris = new []
    {
        "https://www.getpostman.com/oauth2/callback"
    },
    AllowedScopes = { "api", 
                      IdentityServerConstants.LocalApi.ScopeName,
                      IdentityServerConstants.StandardScopes.OpenId,
                      IdentityServerConstants.StandardScopes.Profile,
                      IdentityServerConstants.StandardScopes.Email
    },

}

排查与解决要点

1. 缺少控制器路由映射(最可能原因)

ASP.NET Core不会自动映射控制器路由,必须在Program.cs的管道配置中添加:

app.MapControllers();

注意中间件顺序:需要放在UseAuthentication()、UseAuthorization()之后,确保认证逻辑先执行,同时要在UseIdentityServer()之后配置。

2. 路由大小写匹配问题

你的Action标注为[HttpGet("Signup")](首字母大写),而调用的URL使用了小写signup。虽然.NET Core默认路由不区分大小写,但如果项目配置了强制区分大小写(如通过RouteOptions.LowercaseUrls或LowercaseQueryStrings),会导致匹配失败。可以统一URL和Action的大小写,或者调整路由配置:

builder.Services.Configure<RouteOptions>(options =>
{
    options.LowercaseUrls = true;
    options.LowercaseQueryStrings = true;
});

3. 控制器未被程序集扫描到

如果AccountController位于单独的类库项目中,ASP.NET Core默认不会扫描该程序集,需要显式添加:

builder.Services.AddControllers()
    .AddApplicationPart(typeof(AccountController).Assembly);

4. 中间件顺序错误

确保管道配置的顺序正确,正确的顺序应该是:

var app = builder.Build();

// 其他中间件(如UseHttpsRedirection)
app.UseHttpsRedirection();

app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();

// 映射控制器路由
app.MapControllers();

app.Run();

内容的提问来源于stack exchange,提问作者Code Ratchet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 01:55:25