asp.net-coreasp.net-web-apientity-framework-core

Json parse error while using /login method from WebApiCore Identity provided by microsoft.aspnetcore.identity.entityframeworkcore@8.0.6


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())


Solution

  • after analyzing the exception I've notice that some middleware (an empty implementation) was active and (i think, to figure out why, was stripping out the request body).

    After removing that middleware everything started to parse ok.

    closing the question. i'll update the post on why a custom empty middleware can create this issue later

    EDIT: this issue outlining a problem with access to the request body in a custom middleware explains that the body of the request is a stream and that can cause problems if we try to read it.

    Reading request body in middleware for .Net 5

    As the body is read the stream position will be updated to the end of stream not allowing any more reads. the stream can be rewind but that is not a good practice as the body can be very big.