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