确认在应用程序项目和身份验证服务器项目中都安装了Microsoft.AspNetCore.Authentication.JwtBearer包。
更改应用程序项目的运行配置(launchSettings.json)中的"applicationUrl"为"http://localhost:5000",同时修改身份验证服务器项目的运行配置(Properties/launchSettings.json)中的"applicationUrl"为"http://localhost:5001"。
将应用程序项目的appsettings.json文件中的"IdentityServerBaseUrl"设置为"http://localhost:5001",并将身份验证服务器项目的appsettings.json文件中的"ApplicationUrl"设置为"https://localhost:5001"。
确认在应用程序项目中的Startup.cs文件中添加了以下代码:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = Configuration["IdentityServerBaseUrl"];
options.Audience = "api1";
});
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});