Secure API .net core 3.1(从 2.2 升级 api)和 identityserver 不再有效,我的中间件是否正确?

Secure API .net core 3.1 (upgraded api from 2.2) with identityserver no longer works, is my middleware correct?

我有一个 API (.net core 2.2) 受保护并且可以与身份服务器一起正常工作。

我需要将此 API 升级到 .net core 3.1。所以我重新开始了一个核心 3.1 API 项目,并添加了控制器、dbContext 和中间件……我试图让中间件尽可能接近 2.2(这可能是我的问题)但我我在用 [Authorize] 修饰的方法上收到 401 unauthorized(甚至没有声明任何角色或策略,只是一个简单的授权)。如果我取消 [Authorize] 这工作正常......所以这就是问题所在。

我试图找到从中间件 IdentityServer 的角度将 API 从 2.2 升级到 3.1 的示例,但只能找到升级 IdentityServer 本身的示例(不是受保护的 API ) 在这些版本之间。

我也分析了API方法中的User声明,看起来都很好。这在 2.2 上工作正常,所以我认为这一定与中间件有关,但我不确定......有人可以指出我正确的方向吗?这是我的启动文件:

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using IdentityServer4.AccessTokenValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using HMS.API.Services;
using HMS.Global.Repository;

namespace HMS.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            services.AddControllers();

            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            { 
                options.Authority = "https://????.???????.com"; // Removed for this post
                options.ApiName = "hmsapi";
                
            });

            services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("HMSConnection")));

            // Create policy
            services.AddAuthorization(authorisationOptions =>
            {
                authorisationOptions
                .AddPolicy(
                    "HMSADMINPOLICY",
                    policyBuilder =>
                    {
                        policyBuilder.RequireAuthenticatedUser();
                        policyBuilder.RequireClaim("role", "HMSADMIN");                        
                    });

                authorisationOptions
                .AddPolicy(
                    "HMSSTANDARDPOLICY",
                    policyBuilder =>
                    {
                        policyBuilder.RequireAuthenticatedUser();
                        policyBuilder.RequireClaim("role", "HMSSTANDARD", "HMSADMIN");
                    });
            });

            services.AddSingleton<IUserService, UserService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

我会使用 Fiddler 来调试流量 to/from 我的客户端应用程序,尤其是查看从 IdentityServer 实际返回的声明。

如果你在使用[Authorize]时得到401 not authorized,那么你并没有真正登录。所有登录的用户都应该通过[Authorize]。

如果您也升级到 IdentityServer4 v4.0x,那么您还需要添加 ApiScopes(v4.x 中的新功能)