【发布时间】:2021-10-11 08:42:36
【问题描述】:
我使用 .net 5 web api 来创建我的应用程序。 我将 [Authorize] 放在我的控制器顶部。 我使用 Identityserver4 作为身份验证服务器。(本地主机:5000) 这是我的 startup.cs 类:
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.AddControllers();
services.AddDbContext<MyContext>(opts =>
opts.UseInMemoryDatabase("MyDb"));
services.AddAuthentication("bearer")
.AddIdentityServerAuthentication(opts =>
{
opts.Authority = "http://locallhost:5000";
opts.RequireHttpsMetadata = false;
opts.ApiName = "myApi";
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "CustomerApi", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CustomerApi v1"));
}
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
但我得到以下错误: error
请指导我解决我的问题。 我搜索了很多时间,但答案是针对 .net 3.1 的。
【问题讨论】:
标签: asp.net-web-api identityserver4 openapi asp.net-core-5.0