Created
April 2, 2025 09:39
-
-
Save dj-nitehawk/65b78b08075fae3070e9d30e2a59f4c1 to your computer and use it in GitHub Desktop.
Update JWT Signing Key during runtime
This file contains hidden or 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 bld = WebApplication.CreateBuilder(args); | |
bld.Services | |
.Configure<JwtSigningOptions>(s => s.SigningKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") //must register signing options | |
.Configure<JwtCreationOptions>(c => c.SigningKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") //optional | |
.SwaggerDocument() | |
.AddAuthenticationJwtBearer(s => { }) //no need to specify signing options here due to above | |
.AddAuthorization() | |
.AddFastEndpoints(); | |
var app = bld.Build(); | |
app.UseAuthentication() | |
.UseAuthorization() | |
.UseFastEndpoints() | |
.UseSwaggerGen(); | |
app.Run(); |
This file contains hidden or 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 TokenEndpoint : EndpointWithoutRequest | |
{ | |
public override void Configure() | |
{ | |
Get("token"); | |
AllowAnonymous(); | |
} | |
public override async Task HandleAsync(CancellationToken c) | |
{ | |
await SendAsync(JwtBearer.CreateToken(o => o.User["username"] = "usr001")); | |
} | |
} | |
sealed class ProtectedEndpoint : EndpointWithoutRequest | |
{ | |
public override void Configure() | |
{ | |
Get("protected"); | |
Claims("username"); | |
} | |
public override async Task HandleAsync(CancellationToken c) | |
{ | |
await SendAsync("hello!"); | |
} | |
} | |
sealed class ChangeSigningKeyEndpoint(IOptions<JwtSigningOptions> jwtSigningOpts, IOptions<JwtCreationOptions> jwtCreationOpts) : EndpointWithoutRequest | |
{ | |
//this is just a demonstration of programatically updating the jwt signing key during runtime. | |
//basically you need to obtain an IOptions<Jwt*Options> instance from the DI container. | |
public override void Configure() | |
{ | |
Get("change-signing-key/{letter}"); | |
} | |
public override async Task HandleAsync(CancellationToken c) | |
{ | |
switch (Route<string>("letter")) | |
{ | |
case "x": | |
jwtSigningOpts.Value.UpdateSigningKey("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | |
jwtCreationOpts.Value.SigningKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
break; | |
case "y": | |
jwtSigningOpts.Value.UpdateSigningKey("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"); | |
jwtCreationOpts.Value.SigningKey = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"; | |
break; | |
default: | |
await SendAsync("invalid letter!"); | |
return; | |
} | |
await SendAsync("runtime updating of jwt signing key is successful!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment