Identity Server 4令牌到期Api与ClientApp

问题描述 投票:0回答:1

我当前正在构建具有身份验证/授权的WebApp来访问它,并访问多个WebAPI,所有这些都指向Identity Server 4主机。我已经遵循了IdentityServer4及其演示的官方文档,并进行了客户端身份验证,令牌生成,用户登录,使用令牌成功调用API的操作,显然,这些方法都可以正常工作,但是最近我注意到,经过一段时间的不活动之后,对API开始接收401,但客户端应用程序仍使用相同的令牌启动。

是这样的:

  1. 启动带有调试功能的浏览器
  2. 使用某些用户登录
  3. 进入一个调用一个API为其检索数据的视图
  4. 保持导航和测试,其他一切正常

现在是问题(在上一步4之后)

  1. 停止调试,但保持浏览器正常运行(保持cookie)
  2. 更改代码,实现新功能(基本上需要一段时间)
  3. 再次启动调试
  4. 在已经打开的浏览器上使用相同的会话/ cookie,尝试在应用程序上导航正常,并且不需要重新登录
  5. 导航到将使用当前令牌调用该API的视图,以前没有时会显示401

我发现令牌已过期,Visual Studio输出指出了该令牌(还检查了https://jwt.io/上的令牌,我可以确认日期时间)。为什么相同的令牌对于ClientApp可以正常工作,而调用API却不能呢?由于API的调用,我是否需要手动生成新令牌?

我正在使用的配置是:

-客户申请---

new Client
{
    ClientId = "idWebApp",
    ClientSecrets = new List<Secret> { new Secret("secret".Sha256()) },

    AllowedGrantTypes = GrantTypes.Hybrid,
    AllowAccessTokensViaBrowser = false,

    EnableLocalLogin = true,

    RedirectUris = { "http://localhost:5901/signin-oidc" },
    FrontChannelLogoutUri = "http://localhost:5901/signout-oidc",
    PostLogoutRedirectUris = { "http://localhost:5901/signout-callback-oidc" },

    AllowOfflineAccess = true,

    AllowedScopes = new List<string>
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.OfflineAccess,
        "apiAccess",
    },

    RequireConsent = false,
}

-API资源---

(只需使用简单的ctor来初始化'Name')

new ApiResource("apiAccess")

-自定义声明---

new IdentityResource()
{
    Name = "appCustomClaims",
    UserClaims = new List<string>()
    {
        "customRole"
    }
}

-ClientClient的启动代码-]

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
    .AddCookie("Cookies")
    .AddOpenIdConnect("oidc", options =>
    {
        options.Authority = "http://localhost:5900";
        options.RequireHttpsMetadata = false;

        options.ClientId = "idWebApp";
        options.ClientSecret = "secret";
        options.ResponseType = "code id_token";

        options.Scope.Add("profile");
        options.Scope.Add("offline_access");
        options.ClaimActions.MapUniqueJsonKey("offline_access", "offline_access");

        options.Scope.Add("appCustomClaims");
        options.ClaimActions.MapJsonKey("customRole", "customRole");

        options.Scope.Add("apiAccess");

        options.GetClaimsFromUserInfoEndpoint = true;
        options.SaveTokens = true;
        options.TokenValidationParameters.RoleClaimType = "customRole";
    });

我当前正在构建具有身份验证/授权的WebApp来访问它,并访问多个WebAPI,所有这些都指向Identity Server 4主机。我遵循了官方文档...

authorization identityserver4 access-token
1个回答
0
投票

为什么相同的令牌在调用API时对ClientApp正常工作不?

© www.soinside.com 2019 - 2024. All rights reserved.