While configuring authentication and autorization with web.api endpoints and using:
\packages\microsoft.aspnetcore.identity.entityframeworkcore\8.0.6 \packages\microsoft.entityframeworkcore.sqlserver\8.0.6 \packages\microsoft.aspnetcore.spaproxy\8.0.6\
I get the following error while trying to call any of the api methods created: (ex. /login ou /register)
Microsoft.AspNetCore.Http.BadHttpRequestException: Failed to read parameter "LoginRequest login" from the request body as JSON. ---> System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0. ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.`
my configuration is as follows:
var myAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy(name: myAllowSpecificOrigins,
policy => {
policy.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true)
.AllowCredentials();
});
});
// Add services to the container.
builder.Services.AddSingleton<IDictionary<string, UserConnection>>(opts => new Dictionary<string, UserConnection>());
builder.Services.AddSingleton<IDictionary<string, ChatRoom>>(opts => new Dictionary<string, ChatRoom>());
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSignalR();
builder.Services.AddDbContext(
options => options.UseSqlServer("Server=DESKTOP-EI90NJF\SQLEXPRESS;User Id=WebChatUser;Password=Password123;Database=WebChat")
);
builder.Services.AddIdentityCore()
.AddEntityFrameworkStores()
.AddApiEndpoints();
builder.Services.AddAuthentication().AddBearerToken(IdentityConstants.BearerScheme);
builder.Services.AddAuthorizationBuilder();
var app = builder.Build();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseCors(myAllowSpecificOrigins);
app.UseMiddleware();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapHub("/chatHub");
app.MapFallbackToFile("/index.html");
app.MapIdentityApi();
app.MapControllers();
app.Run();
EDITED: the test was done using the swagger ui rendered by the application and configured using (builder.Services.AddSwaggerGen(); app.UseSwagger(); app.UseSwaggerUI())