C# WebApi服务器启动后仅访问IdentityServer一次

C# WebApi服务器启动后仅访问IdentityServer一次,c#,identityserver4,webapi,C#,Identityserver4,Webapi,你好 我请求具有IdentityServer的访问令牌 启动webapi服务器后,我正在访问我的API 之后,使用当前的访问令牌,我可以访问我的api,即使IdentityServer已关闭 我的问题是:这是正确的工作逻辑吗?不应该在每次调用我的api时调用IdentityServer并检查访问令牌吗 如果没有,如果我的用户凭据已更改并且我需要续订令牌,我如何拒绝访问令牌 Startup.cs: public void Configure(IApplicationBuilder app, IWe

你好

  • 我请求具有IdentityServer的访问令牌
  • 启动webapi服务器后,我正在访问我的API
  • 之后,使用当前的访问令牌,我可以访问我的api,即使IdentityServer已关闭
  • 我的问题是:这是正确的工作逻辑吗?不应该在每次调用我的api时调用IdentityServer并检查访问令牌吗

    如果没有,如果我的用户凭据已更改并且我需要续订令牌,我如何拒绝访问令牌

    Startup.cs:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                app.UseRouting();
                app.UseAuthentication();
                app.UseAuthorization();
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers().RequireAuthorization("ApiScope");
                    //.RequireAuthorization("AdminSecure");
                });
            }
    
    public void ConfigureServices(IServiceCollection services)
            {
               services.AddAuthentication("Bearer")
                    .AddJwtBearer("Bearer", options =>
                    {
                        options.Authority = Configuration.GetValue<string>("IdentityServerUrl");
                        options.TokenValidationParameters = new TokenValidationParameters
                        {
                            ValidateAudience = false
                        };
                    });
                services.AddAuthorization(options =>
                {
                    options.AddPolicy("ApiScope", policy =>
                    policy.RequireClaim("client_id", "secret"));
    
                    options.AddPolicy("UserSecure", policy =>
                    policy.RequireClaim("roleType", "userCode"));
    
                    options.AddPolicy("AdminSecure", policy =>
                    policy.RequireClaim("roleType", "adminCode"));
                });
    }
    
    public void配置(IApplicationBuilder应用程序,IWebHostEnvironment环境)
    {
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(端点=>
    {
    endpoints.MapController().RequireAuthorization(“ApiScope”);
    //.要求重新授权(“AdminSecure”);
    });
    }
    public void配置服务(IServiceCollection服务)
    {
    服务。添加身份验证(“承载人”)
    .AddJwtBearer(“Bearer”,选项=>
    {
    options.Authority=Configuration.GetValue(“IdentityServerUrl”);
    options.TokenValidationParameters=新的TokenValidationParameters
    {
    ValidateAudience=false
    };
    });
    services.AddAuthorization(选项=>
    {
    options.AddPolicy(“ApiScope”,policy=>
    政策。要求收回(“客户id”、“机密”);
    options.AddPolicy(“UserSecure”,policy=>
    policy.requirecall(“roleType”、“userCode”);
    options.AddPolicy(“AdminSecure”,策略=>
    policy.requirecall(“roleType”、“adminCode”);
    });
    }
    
    这是正确的行为,您的API可以从IdentityServer脱机工作。API只需要从IdentityServer下载签名密钥,默认情况下API会将它们缓存24小时,以便验证它们是否有效

    您描述的问题是通过使用短期访问令牌来解决的,比如1-10分钟?然后在客户端中使用刷新令牌来更新令牌


    或者,您的API可以手动要求检查访问令牌是否仍然有效。

    太好了!您可以在此处阅读有关刷新令牌的内容: