Skip to content

Instantly share code, notes, and snippets.

View dj-nitehawk's full-sized avatar

Dĵ ΝιΓΞΗΛψΚ dj-nitehawk

View GitHub Profile
@dj-nitehawk
dj-nitehawk / ClaimTransformation.cs
Last active December 9, 2023 07:17
Dynamic claim/permission hydration instead of embedding everything in JWT Token
sealed class UserPermissionHydrator(UserPermissionService userPermissionService) : IClaimsTransformation
{
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
var userId = principal.Claims.FirstOrDefault(c => c.Type == "UserId")?.Value;
ArgumentNullException.ThrowIfNull(userId);
var userPermissions = await userPermissionService.GetPermissionsForUser(userId);
if (userPermissions.Length != 0)
@dj-nitehawk
dj-nitehawk / Program.cs
Last active October 23, 2023 13:09
Using old style app host-builder with `Startup.cs`
public class Program
{
public static void Main(string[] args)
=> CreateHostBuilder(args).Build().Run();
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
}
@dj-nitehawk
dj-nitehawk / GetTokenEndpoint.cs
Created October 22, 2023 08:04
Antiforgery Token Usage
sealed class GetAfTokenEndpoint : EndpointWithoutRequest
{
public IAntiforgery Antiforgery { get; set; }
public override void Configure()
{
Get("anti-forgery-token");
AllowAnonymous();
}
@dj-nitehawk
dj-nitehawk / Instructions.md
Last active April 14, 2024 07:31
How to import FE Live-Templates for JetBrains Rider

JetBrains Rider LiveTemplates import procedure

  1. Save the following LiveTemplates.DotSettings file to your computer (by right-clicking and selecting Save Link As).
  2. Open any project in Rider.
  3. Click Settings > Manage Layers (located in the left bottom corner).
  4. Right click This Computer > Import From > Import From File
  5. Select the downloaded LiveTemplates.DotSettings
  6. Click close.

That's it... These shortcuts should now be available for you to scaffold code snippets in Rider.

@dj-nitehawk
dj-nitehawk / Program.cs
Created October 6, 2023 10:43
Unit testing an endpoint that executes a command
sealed class MyRequest
{
public int Id { get; set; }
}
public sealed class MyCommand : ICommand<int>
{
public string Identity { get; set; }
}
@dj-nitehawk
dj-nitehawk / AddCustomHeader.cs
Created September 18, 2023 02:07
Customizing Swagger Spec With An IOperationProcessor
internal sealed class AddCustomHeader : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
{
var hdrParameter = new OpenApiParameter()
{
Name = "x-custom",
Kind = OpenApiParameterKind.Header,
IsRequired = true,
Type = JsonObjectType.String,
@dj-nitehawk
dj-nitehawk / Program.cs
Last active September 13, 2023 06:09
Showing deprecated endpoint versions in Swagger
var bld = WebApplication.CreateBuilder();
bld.Services
.AddFastEndpoints()
.SwaggerDocument(o =>
{
o.DocumentSettings = s =>
{
s.DocumentName = "Initial Release";
s.Version = "v0";
};
@dj-nitehawk
dj-nitehawk / Program.cs
Created September 8, 2023 09:06
JsonPatch usage with Swagger UI support
using FastEndpoints.Swagger;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NJsonSchema.Annotations;
using System.Text.Json;
var bld = WebApplication.CreateBuilder();
bld.Services
@dj-nitehawk
dj-nitehawk / Program.cs
Created September 7, 2023 03:27
Storing `IJobStorageRecord` and `IEventStorageRecord` via EntityFramework Core
using FastEndpoints;
using MessagePack;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
public class JobRecord : IJobStorageRecord
{
public Guid Id { get; set; }
public string QueueID { get; set; }
public object Command { get; set; }
@dj-nitehawk
dj-nitehawk / Program.cs
Last active January 9, 2024 10:55
Request DTO inheritance with Validator composition
public class BaseRequest
{
public string? Id { get; init; }
}
public class BaseRequestValidator : Validator<BaseRequest>
{
public BaseRequestValidator()
{
RuleFor(x => x.Id)