1

Just starting out with .net core minimal API and trying to solve all the little issues that will have to work before it's usable in my situation.

This API will be accessible from multiple tenants so I need to check the calling domain on every request (including when authenticating via user/pass) and then set some variables that need to be accessible from all routes.

Can anyone point me in the right direction because I can't find any information on how this is achieved.

Thanks

UPDATE - some examples of where the middleware works and doesnt

So this is the middleware

app.Use((context, next) =>
{
    // Lets assume the domain info is in the query string
    context.Items["domain"] = context.Request.Query["domain"];
    return next();

});

and this code works just fine

app.MapGet("/", async handler =>
{
    // Get the domain and then return back the formatted string with the domain name
    var domain = handler.Items["domain"];
    await handler.Response.WriteAsJsonAsync($"Hello World! {domain}");
});

When I have an endpoint decorated with [AlowAnonymous] I have to put handler in brackets like this

app.MapGet("/whatever",
[AllowAnonymous] async (handler)  =>
{
    // Get the domain and then return back the formatted string with the domain name
    var domain = handler.Items["domain"];
    await handler.Response.WriteAsJsonAsync($"Hello World! {domain}");
});

If I have multiple class objects it borks (this has the httprequest, my db class and a login class). The error is Delegate 'RequestDelegate' does not take 4 arguements.

app.MapGet("/whatever2",
[AllowAnonymous] async (handler, HttpRequest request, SqlConnection db, Login login) =>
{
    // Get the domain and then return back the formatted string with the domain name
    var domain = handler.Items["domain"];
    await handler.Response.WriteAsJsonAsync($"Hello World! {domain}");
}); 

Thanks

Rolf Herbert
  • 45
  • 1
  • 8

1 Answers1

1

You can add a piece of middleware in the request pipeline to fetch any details needed for mapped APIs. For example...

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.Use((context, next) =>
{
    // Lets assume the domain info is in the query string
    context.Items["domain"] = context.Request.Query["domain"];
    
    return next();
});

app.MapGet("/", async handler =>
{
    // Get the domain and then return back the formatted string with the domain name
    var domain = handler.Items["domain"];
    await handler.Response.WriteAsJsonAsync($"Hello World! {domain}");
});

app.Run();

The above adds middleware that runs first before attempting to map to any of the endpoints.

ajawad987
  • 4,439
  • 2
  • 28
  • 45
  • Please note I updated the code to actually work now. Apologies! :D – ajawad987 Jul 11 '22 at 22:23
  • Thanks for this. Where does handler come from and I cant seem to add it to my own app.MapPost/Gets where they are decorated or passing classes in like app.MapPost("/security/getToken", [AllowAnonymous] (HttpRequest request, SqlConnection db, Login login) => I cant add handler here so its not available. – Rolf Herbert Jul 12 '22 at 08:06
  • Can you update your post to include code your trying? I'm not following why your not able to see the `handler` parameter. – ajawad987 Jul 12 '22 at 13:32
  • Updated the original post with some examples – Rolf Herbert Jul 13 '22 at 06:35
  • 1
    Hi there, figured it out, when adding multiple parameters they all have to have a type declared or all match.. so HttpContext handler works. Thanks, I'll go away and experiment with the middleware now. – Rolf Herbert Jul 13 '22 at 07:37