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

ASP.NET Core Blazor WebAssembly集成Identity Server4 Postman测试获取Token问题

配置调整步骤

1. 修正IdentityServer Postman客户端配置

你当前新增的Postman客户端缺少必要配置项,修改后的代码如下:

services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options => {
        options.IdentityResources["openid"].UserClaims.Add("name");
        options.ApiResources.Single().UserClaims.Add("name");
        options.IdentityResources["openid"].UserClaims.Add("role");
        options.ApiResources.Single().UserClaims.Add("role");
        options.Clients.Add(new IdentityServer4.Models.Client()
        {
            ClientId = "postman",
            AllowedGrantTypes = GrantTypes.Code,
            RequirePkce = true, // 开启PKCE校验,适配Postman默认授权码流配置
            RequireClientSecret = false, // 浏览器公开流程可关闭密钥校验,避免密钥泄漏
            AllowOfflineAccess = true,
            AllowAccessTokensViaBrowser = true, // 允许浏览器流程返回令牌
            RequireHttps = false, // 本地HTTP开发时关闭HTTPS强制校验,生产环境请删除该行
            RedirectUris = { "http://localhost:21402/signin-oidc", "https://oauth.pstmn.io/v1/browser-callback" },
            PostLogoutRedirectUris = { "http://localhost:21402/" },
            FrontChannelLogoutUri = "http://localhost:21402/signout-oidc",
            AllowedScopes =
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.Email,
                "Onero.ServerAPI"
            },
        });
    });

2. 新增CORS配置解决跨域拦截

Postman回调域名会触发跨域校验,需要在服务端放行:

ConfigureServices中新增CORS策略

services.AddCors(options =>
{
    options.AddPolicy("AllowPostman", policy =>
    {
        policy.WithOrigins("https://oauth.pstmn.io")
              .AllowAnyHeader()
              .AllowAnyMethod()
              .AllowCredentials();
    });
});

Configure中启用CORS中间件

确保中间件顺序正确,示例如下:

app.UseRouting();

app.UseCors("AllowPostman"); // 放在UseRouting之后,认证中间件之前

app.UseAuthentication();
app.UseIdentityServer(); // 必须放在UseAuthentication之后,UseAuthorization之前
app.UseAuthorization();

// 后续端点映射配置
app.MapControllers();

3. Postman OAuth 2.0配置对齐

按以下参数配置Postman的授权页:

  • 授权类型选择 Authorization Code (With PKCE)
  • 回调URL填写 https://oauth.pstmn.io/v1/browser-callback,和服务端配置完全一致
  • 授权URL填写 http://<你本地服务地址+端口>/connect/authorize
  • Access Token URL填写 http://<你本地服务地址+端口>/connect/token
  • Client ID填写 postman,Client Secret留空(对应上面关闭客户端密钥校验的配置)
  • Scope填写 openid profile email Onero.ServerAPI offline_access
  • 勾选 Authorize using browser 选项

常见报错对应修复说明

  • 浏览器弹出后无法完成流程:90%是跨域配置缺失、PKCE未开启、回调地址不匹配三个原因导致,按上面步骤逐一核对即可
  • DenyAnonymousAuthorizationRequirement报错:一般是中间件顺序错误,或者请求时没有携带有效Token,确认中间件顺序和Token有效性即可
  • unauthorized_client报错:一般是授权类型和客户端配置的AllowedGrantTypes不匹配,或者Client ID填写错误,优先使用推荐的授权码+PKCE模式,不要使用密码模式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 08:15:08