gpt4 book ai didi

asp.net-core - .NET & Swagger 开始出现 401 错误

转载 作者:行者123 更新时间:2023-12-04 15:55:52 25 4
gpt4 key购买 nike

我已经为我的 .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 url 我收到 401 错误。
我不需要对 Swagger 文档和 UI 进行任何身份验证,所有这些都是公开的。

这可能与 .NET Core 2.1.2有关吗? ?

使用 Swashbuckle.AspNetCore 3.0.0
欢迎任何想法。

最佳答案

对于那些可能遇到此类问题的人来说,修复实际上非常简单。对于我实际需要身份验证的新 webApi Controller 之一,我忘记添加属性 [Route("api/[controller]")] ,因此 /swagger url 指向该 Controller ,因此出现 401 错误。去搞清楚 :)

关于asp.net-core - .NET & Swagger 开始出现 401 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51712218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com