Annabelle
Annabelle
CC#
Created by Annabelle on 3/16/2025 in #help
JWT still validates token if present in headers even for un-protected route
I'm having issue with JWT middleware even if I mark route with [AllowAnonymous] if I pass invalid JWT token it still validates token, and if route is not protected example /sign-in, then I do not want those requests to be rejected. It should only check for routes that are protected. JWT extension code https://paste.ofcode.org/Kv5vPnxcGyryzJ5tbqaAYY
7 replies
CC#
Created by Annabelle on 2/17/2025 in #help
BsonDocument exceeds the JsonSerializerOptions.MaxDepth setting
Type
[BsonCollection("product_categories")]
public class ProductCategory
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = null!;

[BsonElement("name")]
[BsonRequired]
public required string Name { get; set; }

[BsonElement("icon")]
public ImageAsset? Icon { get; set; }

[BsonElement("schema")]
[BsonExtraElements]
public required BsonDocument Schema { get; set; }

[BsonElement("is_active")]
public bool IsActive { get; set; }

[BsonElement("created_at")]
[BsonRequired]
public DateTime CreatedAt { get; set; }

[BsonElement("updated_at")]
[BsonRequired]
public DateTime UpdatedAt { get; set; }
}
[BsonCollection("product_categories")]
public class ProductCategory
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = null!;

[BsonElement("name")]
[BsonRequired]
public required string Name { get; set; }

[BsonElement("icon")]
public ImageAsset? Icon { get; set; }

[BsonElement("schema")]
[BsonExtraElements]
public required BsonDocument Schema { get; set; }

[BsonElement("is_active")]
public bool IsActive { get; set; }

[BsonElement("created_at")]
[BsonRequired]
public DateTime CreatedAt { get; set; }

[BsonElement("updated_at")]
[BsonRequired]
public DateTime UpdatedAt { get; set; }
}
running the project returns System.InvalidOperationException: The depth of the generated JSON schema exceeds the JsonSerializerOptions.MaxDepth setting. I'm using Scalar.AspNetCore for this with default MapOpenApi I tried doing as well but nothing helps.
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.MaxDepth = 256;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.MaxDepth = 256;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
5 replies
CC#
Created by Annabelle on 11/25/2023 in #help
Ocelot/Yarp a valid use-case for the following react/asp.net?
Hello there I'm currently working on application and I want to use C# AspNet for the backend, and for frontend I want react. Issue is regarding the security. I wanted to go with JWT access/refresh token, but try not to use localStorage, and only way would be to use http only cookies and I can only set that inside of C# As the asp net project, and frontend are 2 different domains I cannot easily work with cookies between them. Idea is to use Ocelot/Yarp to create following match: route starts with /api -> redirect to my C# asp net project everything else needs to go to the react side So just a gateway Then authentication would be easier as we can use one domain, and react will be able to fetch session from some route /session Do you think I'm on the right track here, or any better ideas?
17 replies
CC#
Created by Annabelle on 6/26/2023 in #help
❔ Dapper Unit Of Work
So I'm setting up a simple UnitOfWork with dapper For that we need to have a scoped Transaction and Connection
public DapperContext(IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnectionString");
Connection = new NpgsqlConnection(connectionString);
Connection.Open();
Transaction = Connection.BeginTransaction();
}
public DapperContext(IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("DefaultConnectionString");
Connection = new NpgsqlConnection(connectionString);
Connection.Open();
Transaction = Connection.BeginTransaction();
}
we register this as
services.AddScoped<IDapperContext, DapperContext>();
services.AddScoped<IDapperContext, DapperContext>();
I have to open connection with Connection.Open(); before creating a transaction though My question is how good of an idea is this? Is there any better way of doing this without blocking?
6 replies
CC#
Created by Annabelle on 6/13/2023 in #help
❔ Tests best practice
Hello I'm interested in learning more about testing in general like unit tests and other kind of tests I try looking at guides online, but they all show really bad examples that are not just not real. I would like to unit test my ASPNet Api's database logic that seems to need mocks, but should controller be tested? Like if we have ProductController with method CreateProduct(Request) do we test this or just logic inside it? Like services/Repos Does the testing of API fall under different kind of tests like E2E testing for frontend
119 replies