dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.52k stars 3.13k forks source link

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

Closed pfigueiredo closed 1 week ago

pfigueiredo commented 2 weeks ago

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

pfigueiredo commented 2 weeks ago

the test was done using the swagger UI rendered by the application and configured (builder.Services.AddSwaggerGen(); app.UseSwagger(); app.UseSwaggerUI()) other controllers don't return any error like the /WeatherForecast

roji commented 1 week ago

@pfigueiredo this seems like some sort of network/environmental issue that doesn't have anything to do with EF Core.