我已经为我的.NET Core webApi应用程序配置了Swagger,这是它的配置
public void ConfigureServices(IServiceCollection services)
{
services.AddAuth();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.Build());
});
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new Info
{
Version = "v1",
Title = "XXX Synchronization API",
Description = "XXX Synchronization API",
TermsOfService = "None",
Contact = new Contact { Name = "XXX Inc.", Email = "XXXX", Url = "XXX.com" }
});
c.IncludeXmlComments(GetXmlCommentsPath());
c.DescribeAllEnumsAsStrings();
});
services.AddSingleton<IHashProvider, Pbkdf2Version1HashProvider>();
services.AddSingleton<IPasswordHasher, PasswordHasher>();
services.AddTransient<IAuthenticationService, GfdAuthenticationService>();
services.AddSingleton(new GfdSqlServerDatabase(Gfd2Configuration.Settings.GfdSqlServerConnectionString));
services.AddSingleton(new MySqlSyncDatabase(Gfd2Configuration.Settings.AwsMySqlConnectionString));
services.AddTransient<SyncRepository, AwsMySqlSyncRepository>();
services.AddHostedService<QueuedHostedService>();
services.AddSingleton<IBackgroundTaskQueue, BackgroundTaskQueue>();
services.AddSingleton<ArchivedJobProcessorBase, ArchivedJobProcessorFeatures>();
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseCors("CorsPolicy");
app.UseMvc();
app.UseSwagger();
#if DEBUG
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "XXXX Synchronization API V1"); });
#else
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/sync/swagger/v1/swagger.json", "XXX Synchronization API V1"); });
#endif
}
一切正常。出于某种原因,今天早上我每次打开/swagger
网址时,都会收到401错误。
我不需要Swagger文档和UI的任何身份验证,所有都是公开的。
这与.NET Core 2.1.2
有关吗?
使用Swashbuckle.AspNetCore 3.0.0
欢迎提出任何想法。
答案 0 :(得分:0)
对于那些可能遇到这种问题的人,修复实际上非常简单。对于实际上需要身份验证的我的新webApi控制器之一,我忘记添加属性[Route("api/[controller]")]
,因为/swagger
url指向该控制器,因此出现401错误。去图:)