IdentityServer4发现文档无格式显示问题求助
解决IdentityServer4发现端点JSON未格式化的问题
嘿,这个问题我之前调试IdentityServer4的时候也碰到过,其实调整下ASP.NET Core的JSON序列化配置就能搞定,下面给你两种实用的解决方法:
方法一:全局配置JSON格式化输出
在你的项目配置文件里(.NET Core 3.x/.NET 5用Startup.cs,.NET 6+用Program.cs),找到服务配置的部分,开启JSON缩进格式化:
适用于.NET Core 3.x / .NET 5
public void ConfigureServices(IServiceCollection services) { // 其他服务配置代码... services.AddControllersWithViews() .AddJsonOptions(options => { // 开启JSON缩进格式化 options.JsonSerializerOptions.WriteIndented = true; }); // IdentityServer相关配置 services.AddIdentityServer() .AddInMemoryClients(Config.Clients) .AddInMemoryApiScopes(Config.ApiScopes) .AddDeveloperSigningCredential(); }
适用于.NET 6+(顶级语句写法)
var builder = WebApplication.CreateBuilder(args); // 其他服务配置代码... builder.Services.AddControllersWithViews() .AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; }); builder.Services.AddIdentityServer() .AddInMemoryClients(Config.Clients) .AddInMemoryApiScopes(Config.ApiScopes) .AddDeveloperSigningCredential(); // 其他配置代码...
方法二:仅针对IdentityServer端点配置(可选)
如果不想全局修改JSON格式,也可以单独给IdentityServer的发现端点做定制,但这个需要自定义端点处理逻辑,步骤相对繁琐,开发环境用全局配置就足够满足需求了。
注意事项
- 这个配置只建议在开发环境开启,格式化后的JSON会增大响应体积,生产环境保持紧凑格式更高效。
- 你看到官方文档里的格式化效果,就是因为示例项目在开发环境中开启了这个配置。
重启项目后再访问http://localhost:5000/.well-known/openid-configuration,就能看到和文档里一样的缩进格式化JSON啦!
内容的提问来源于stack exchange,提问作者Kirsten
相关产品推荐
相关产品推荐

