【发布时间】:2021-08-28 00:32:13
【问题描述】:
当我从 Swagger UI 请求任何 API 端点时,给我以下错误
System.InvalidOperationException: 没有 authenticationScheme 指定,并且没有找到 DefaultChallengeScheme。默认 可以使用 AddAuthentication(string defaultScheme) 或 AddAuthentication(Action 配置选项)。
在 Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext 上下文、字符串方案、AuthenticationProperties 属性)
在 Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate 接下来,HttpContext 上下文,AuthorizationPolicy 策略, PolicyAuthorizationResult authorizeResult)
在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文)
在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文)
在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
标题
=======
接受:/
接受编码:gzip、deflate
接受语言:en-US,en;q=0.5
授权:不记名 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOiIzIiwiTG9naW5JZCI6ImFkbWluIiwiVXNlclR5cGVJZCI6IjEiLCJFbWFpbCI6ImEiLCJNb2JpbGUiOiJhIiwianRpIjoiMWU1MDY3ODAtMWRjNS00MDYzLWFkMTktMDdlMjg4MzAxOWVjIiwiZXhwIjoxNjIzNDYzNjQ4LCJpc3MiOiJlZHVjYXJlLmNvbSIsImF1ZCI6ImVkdWNhcmUuY29tIn0.G2-D_oIdwUDw_3iz87jxWBIMabFpLlR5ASjCr109kNM P>
连接:保持活动
主机:本地主机:21068
引用者:http://localhost:21068/swagger/index.html
下面给出了 Swagger 的配置
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Edu Care API", Version = "v1" });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "Please insert the authorization token into field",
Name = "Authorization",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Edu Care API v1");
//c.RoutePrefix = string.Empty;
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
我的基本控制器如下
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class BaseController : ControllerBase
{
}
我的 API 端点如下
[Route("api/[controller]")]
[ApiController]
public class AcademicFeeConfigurationController : BaseController
{
[HttpGet]
[Route("[action]")]
public IActionResult GetFeeRelatedAllSli()
{
try
{
object sli = new
{
FeeTypeSli = new FeeTypeService(context).FeeTypeSli(),
ClassInfoSli = new ClassInfoService(context).ClassInfoSli(),
SectionSli = new SectionInfoService(context).SectionInfoSli(),
AcademicSessionSli = new AcademicSessionInfoService(context).AcademicSessionSli(),
};
return Ok(sli);
}
catch (Exception ex)
{
ResponseModel.Notification = UtilityHelper.CreateNotification(Helpers.ExceptionMessage(ex), Enums.NotificationType.Error);
return StatusCode(500, ResponseModel);
}
}
}
也许缺少一些东西。任何人都可以提供任何建议/解决方案
【问题讨论】:
-
startup.cs ConfigureServices 方法中是否有任何身份验证设置?看来您没有设置任何身份验证 sechma 并在 api 控制器中使用 Authorize。
标签: asp.net-core .net-core swagger swashbuckle.aspnetcore