0

I am new to C#. I have a project using C#, ASP.NET Core 3.1 and Entity Framework.

I have done all the JWT configuration.

    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [HttpPost, Route("delete/{id}")]
    public async Task<ActionResult<Login>> DeleteUsuario(int id)
    .
    .
    .

in Startup.cs

      public void ConfigureServices(IServiceCollection services){
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .
        .

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();
        app.UseAuthentication();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
}

When the token is not sent immediately as a response a 401 error is generated. I would like to return a json like this, when I getting this error:

{"message": "Sign in"}

How can I do it?

Sai Gummaluri
  • 1,340
  • 9
  • 16
yavg
  • 2,761
  • 7
  • 45
  • 115
  • Looks like similar to https://stackoverflow.com/questions/48649717/addjwtbearer-onauthenticationfailed-return-custom-error – talesa82035 Aug 24 '20 at 05:44
  • @talesa82035 thanks! it's almost what I'm looking for, but in the answer to that question, a string is returned, do you know how I can make a json return? – yavg Aug 24 '20 at 06:07
  • just use Json.NET to serialize the object you want to return. – talesa82035 Aug 24 '20 at 06:16

0 Answers0