Open Milad-Rashidi-Git opened 1 month ago
I think middleware is your best option tbh
Or you use some sort of service class that you inject into the routes you know the types you want to mask
app.MapGet("/foo/{id}", string id, IDb db, IMaskService maskService) =>
{
var entity = db.GetById(id);
var maskedEntity = maskService.Mask(entity);
return maskedEntity;
});
I think middleware is your best option tbh
I don't think so because of those reasons I mentioned in the question.
Which specific reason? I don't see why this can't be achieved by middleware with the information presented
Or you use some sort of service class that you inject into the routes you know the types you want to mask
app.MapGet("/foo/{id}", string id, IDb db, IMaskService maskService) => { var entity = db.GetById(id); var maskedEntity = maskService.Mask(entity); return maskedEntity; });
As I mentioned in my question, I need to automate the Data Masking process.
@JoeStead I considered creating a response masking middleware, but it has some issues:
Carter operates at the level you're having issues with. The issues you're facing, carter will have to deal with internally too.
If you want to mask responses, surely the correct solution would be to not send fields, and do any masking on the UI?
Carter operates at the level you're having issues with. The issues you're facing, carter will have to deal with internally too.
If you want to mask responses, surely the correct solution would be to not send fields, and do any masking on the UI?
Are you saying that there is no way to add an ASP.NET Core result filter to Carter?
That is an ASP.NET Core MVC feature, not asn ASP.NET Core feature afaik
You can use endpoint filter. Not sure about result filter
On Mon, 23 Sep 2024 at 17:07, Milad Rashidi @.***> wrote:
Carter operates at the level you're having issues with. The issues you're facing, carter will have to deal with internally too.
If you want to mask responses, surely the correct solution would be to not send fields, and do any masking on the UI?
Are you saying that there is no way to add an ASP.NET Core result filter to Carter?
— Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/363#issuecomment-2368740143, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJXL5JNFHVD22W2KV4TZYA4E7AVCNFSM6AAAAABOU3O6ESVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNRYG42DAMJUGM . You are receiving this because you commented.Message ID: @.***>
@JoeStead Since ASP.NET Core integrates MVC into its structure, even in an API project, you can still leverage filters, including result filters.
dotnet new webapi -n SampleApiWithFilter
cd SampleApiWithFilter
// Filters/MyResultFilter.cs
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
public class MyResultFilter : IAsyncResultFilter
{
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
// Code to execute before the result is processed
// For example, adding a custom header
context.HttpContext.Response.Headers.Add("X-My-Custom-Header", "HeaderValue");
// Continue executing the next result filter or the action result
await next(); // Call the next delegate/middleware in the pipeline
// Code to execute after the result is processed
// You can log results or modify the response if needed
}
}
// Controllers/WeatherForecastController.cs
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
[ApiController]
[Route("[controller]")]
[ServiceFilter(typeof(MyResultFilter))] // Apply filter at the controller level
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(options =>
{
// You can also apply globally with options.Filters.Add(new MyResultFilter());
});
services.AddScoped<MyResultFilter>(); // Registering the filter
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
dotnet run
That template generates an MVC project
You can use endpoint filter. Not sure about result filter
I'm okay with using an Endpoint Filter instead of a Result Filter. How to add it to the carter?
@jchannon
I believe that the issues I mentioned regarding response middleware also apply to Endpoint Filters
.
How can I determine the type of the response DTO model within an IEndpointFilter
?
For instance, how can I ascertain whether this response is of type UserDto
?
No idea I’m afraid
You could look to use Carters IResponseNegotiator will give you object you can do a Gettype on but not sure this is the right thing to do either
On Mon, 23 Sep 2024 at 18:10, Milad Rashidi @.***> wrote:
@jchannon https://github.com/jchannon
I believe that the issues I mentioned regarding response middleware also apply to Endpoint Filters. How can I determine the type of the response DTO model within an IEndpointFilter? For instance, how can I ascertain whether this response is of type UserDto ?
— Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/363#issuecomment-2368880644, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJRA37X2GGL26P7KTSTZYBDOTAVCNFSM6AAAAABOU3O6ESVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNRYHA4DANRUGQ . You are receiving this because you were mentioned.Message ID: @.***>
No idea I’m afraid You could look to use Carters IResponseNegotiator will give you object you can do a Gettype on but not sure this is the right thing to do either
Ok, thanks.
@Milad-Rashidi-Git The solution is simple: Don't use minimal APIs. Use controllers.
I am using Carter (version 8.1.0) and have developed some minimal APIs with CQRS and vertical slice architecture in my .Net Core 8 web application. I need to mask some sensitive data in the response, for which I’ve created a
ResultFilterAttribute
as shown below:This is the
Program.cs
file:To add the
MaskSensitiveDataAttribute
filter to the pipeline, I researched extensively and expected to find something like the following code:However, the
AddCarter
extension method is defined as follows and takes anAction<CarterConfigurator>
, which doesn’t provide an option to add filters:My question is: How can I register this global response masking filter in carter?
If there were a solution to not using reflection, it would be much appreciated as the reflection has a performance overhead.
Please Note: I considered creating a response masking middleware, but it has some issues: