0

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

3
  • It has something to do with the [FromBody] Atribute but i can't still find out what's the problem Commented Jul 7 at 21:15
  • I reproduced your issue returning a 400 badrequest and my data is empty but not null with a blank or enter. What is your data and status code, what is the html you got? Based on the error message the login request's being not recognized is the issue, it would be better if you could share. Commented Jul 9 at 7:18
  • I just call it out of the swagger page generated. the body has: { "email": "[email protected]", "password": "Password" } : status code 400 Commented Jul 9 at 7:50

1 Answer 1

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.