C# JWT承载令牌不适用于ASP.Net Core 3.1+;身份服务器4

C# JWT承载令牌不适用于ASP.Net Core 3.1+;身份服务器4,c#,asp.net,.net,jwt,asp.net-identity,C#,Asp.net,.net,Jwt,Asp.net Identity,我目前正在尝试为im开发的移动应用程序创建web api。 我决定使用ASP.NETCore3.1和附带的Identity服务器来使用OpenIDConnect+PKCE进行身份验证。 用户将通过WebView授权应用程序,访问令牌将用于对api的后续调用。api的控制器用[Authorize]属性修饰 我尝试使用一些搭建的代码进行身份验证,但现在我遇到了困难,因为jwt令牌在某些情况下似乎无法被识别 在框架代码中,启动类中名为services.AddDefaultIdentity()的Conf

我目前正在尝试为im开发的移动应用程序创建web api。 我决定使用ASP.NETCore3.1和附带的Identity服务器来使用OpenIDConnect+PKCE进行身份验证。 用户将通过WebView授权应用程序,访问令牌将用于对api的后续调用。api的控制器用
[Authorize]
属性修饰

我尝试使用一些搭建的代码进行身份验证,但现在我遇到了困难,因为jwt令牌在某些情况下似乎无法被识别

在框架代码中,启动类中名为
services.AddDefaultIdentity()
ConfigureServices
方法。 问题是,这还为标识添加了我不需要/想要的所有默认端点(/Account/Manage/Disable2fa、/Account/Manage/PersonalData等)。 因此,我将代码改为
services.AddIdentity()
,因为我现在读了多次,
AddDefaultIdentity()
显然与
AddIdentity()
相同,但也调用了
.AddDefaultUI()

在测试登录流之后,api控制器在进行更改后不再接受
Authorization:Bearer…
标题,api调用总是被重定向到登录页面,就好像用户没有登录一样

当我将代码更改回AddDefaultIdentity时,JWT承载令牌再次正确身份验证,我可以使用[Authorize]属性访问控制器,但随后我再次遇到默认身份页的问题

我的问题是:如何使用[Authorize]Arribute和JWT Bearer头来保护我的API控制器,而不必包括默认的标识UI

Startup.cs

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Munchify.Web.Data;
using Munchify.Web.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using APIBackend.Web.Services;

namespace APIBackend.Web
{
    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.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = "/Identity/Account/Login";
            });

            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

            services.AddAuthentication()
                .AddIdentityServerJwt();

            services.AddTransient<IEmailSender, EmailSender>();

            services.AddControllersWithViews();
            services.AddRazorPages();

        }

        // 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.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseIdentityServer();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }
    }
}
使用Microsoft.AspNetCore.Authentication;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.EntityFrameworkCore;
使用Munchify.Web.Data;
使用Munchify.Web.Models;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用Microsoft.AspNetCore.Identity;
使用Microsoft.AspNetCore.Identity.UI.Services;
使用apibend.Web.Services;
名称空间apibend.Web
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
services.AddIdentity(options=>options.SignIn.RequireConfirmedAccount=true)
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.configureApplicationOK(选项=>
{
options.LoginPath=“/Identity/Account/Login”;
});
services.AddIdentityServer()
.addapi授权();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddTransient();
services.AddControllersWithViews();
services.AddRazorPages();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller}/{action=Index}/{id?}”);
endpoints.MapRazorPages();
});
}
}
}
我设法修复了它