Trying to add role authorization to minimal api and I dont understand why this simple test is not working. I can confirm that the role works.
In my Program.cs
I have app.ConfigureApi();
in Api.cs
public static class Api
{
public static void ConfigureApi(this WebApplication app)
{
// This works
app.MapGet("/Hello/", [Authorize(Roles = Roles.Manager)] () =>
{
return Results.Ok("hello test");
});
//this works
app.MapGet("/HolaNoAutho/", GetHola);
//This does not work. Errors out.
app.MapGet("/HelloAutho/", [Authorize(Roles = Roles.Manager)]() => GetHola);
}
private static async Task<IResult> GetHola()
{
try
{
return Results.Ok("Hola Test");
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
}
}
Any idea how I can make this work role authorization with minimal api?
The error is:
System.NotSupportedException: Serialization and deserialization of 'System.Func
1[[System.Threading.Tasks.Task
1[[Microsoft.AspNetCore.Http.IResult, Microsoft.AspNetCore.Http.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' instances are not supported. The unsupported member type is located on type 'System.Func1[System.Threading.Tasks.Task
1[Microsoft.AspNetCore.Http.IResult]]'. Path: $.