This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = WebApplication.CreateBuilder(); | |
builder.Services.AddTransient<IAuthorizationHandler, TestHandler>(); //Register your handler | |
builder.Services.AddFastEndpoints(); | |
builder.Services.AddJWTBearerAuth("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | |
builder.Services.AddAuthorization(o => | |
{ | |
//Set the default policy to use your requirements (so you don't have to set the policy on each endpoint) | |
o.DefaultPolicy = new AuthorizationPolicyBuilder() | |
.RequireAuthenticatedUser() | |
.AddRequirements(new OperationAuthorizationRequirement()) //Using built-in requirement for testing here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
JsonPolymorphic(TypeDiscriminatorPropertyName = "_t"), | |
JsonDerivedType(typeof(MultiChoiceQuestionRequest), "mcq"), | |
JsonDerivedType(typeof(RatingQuestionRequest), "rq") | |
] | |
public class BaseQuestionRequest | |
{ | |
public int Id { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseRequest | |
{ | |
public string? Id { get; init; } | |
} | |
public class BaseRequestValidator : Validator<BaseRequest> | |
{ | |
public BaseRequestValidator() | |
{ | |
RuleFor(x => x.Id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CreateToken : EndpointWithoutRequest | |
{ | |
public override void Configure() | |
{ | |
Get("token"); | |
AllowAnonymous(); | |
} | |
public override async Task HandleAsync(CancellationToken ct) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FastEndpoints; | |
using FastEndpoints.Swagger; | |
var bld = WebApplication.CreateBuilder(); | |
bld.Services | |
.AddFastEndpoints() | |
.SwaggerDocument(); | |
var app = bld.Build(); | |
app.UseFastEndpoints() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class GetAfTokenEndpoint : EndpointWithoutRequest | |
{ | |
public IAntiforgery Antiforgery { get; set; } | |
public override void Configure() | |
{ | |
Get("anti-forgery-token"); | |
AllowAnonymous(); | |
} |