When I request any API endpoint from Swagger UI give me the following error
System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions).
at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: /
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOiIzIiwiTG9naW5JZCI6ImFkbWluIiwiVXNlclR5cGVJZCI6IjEiLCJFbWFpbCI6ImEiLCJNb2JpbGUiOiJhIiwianRpIjoiMWU1MDY3ODAtMWRjNS00MDYzLWFkMTktMDdlMjg4MzAxOWVjIiwiZXhwIjoxNjIzNDYzNjQ4LCJpc3MiOiJlZHVjYXJlLmNvbSIsImF1ZCI6ImVkdWNhcmUuY29tIn0.G2-D_oIdwUDw_3iz87jxWBIMabFpLlR5ASjCr109kNM
Connection: keep-alive
Host: localhost:21068
Referer: http://localhost:21068/swagger/index.html
the Swagger configuration is given below
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();
});
}
my base controller as below
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class BaseController : ControllerBase
{
}
my API end point as below
[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);
}
}
}
Missing maybe something. Can anyone give any suggestion/solution
It's not about Swagger your code is missing AddAuthentication(). The example below registers the Authentication schemes (JWT & Cookie) while using the JWT as the default scheme. More info in the Docuementation.
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => Configuration.Bind("JwtSettings", options))
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => Configuration.Bind("CookieSettings", options));