Skip to content

Instantly share code, notes, and snippets.

View NMillard's full-sized avatar
🏠
Working from home

Nicklas Millard NMillard

🏠
Working from home
View GitHub Profile
@NMillard
NMillard / User.cs
Last active February 17, 2021 16:21
Medium 2
public class User {
// Terrible property - this can be set to anything without reason
public string Username { get; set; }
// slightly better, but requires additional field
// and we still wouldn't know why "username2" changed
private string username2;
public string Username2 {
@NMillard
NMillard / Iban.cs
Last active February 17, 2021 16:18
medium article 1
public class Iban {
private readonly string plainIban;
private Iban(string iban) {
if (!Validate(iban)) throw new ArgumentException();
plainIban = iban;
}
private bool Validate(string ibanToValidate) {
// validation (check length, validate check digits and sum, etc.)
@NMillard
NMillard / Program.cs
Created June 27, 2020 06:35
If-Else vs Dynamic Type Discovery
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
namespace DesignPatterns.DynamicStrategy {
class Program {
static void Main(string[] args) {
/*
@NMillard
NMillard / AuthorizationController.cs
Last active August 4, 2022 09:17
Endpoints to create and verify asymmetric JWT
// ... imports
namespace Authentication.WebClient.Controllers {
[ApiController]
[Route("api/[controller]/[action]")]
public class AuthorizationController : ControllerBase {
private readonly IConfiguration configuration;
public AuthorizationController(IConfiguration configuration) {
this.configuration = configuration; // Needed to access the stored JWT secret key
@NMillard
NMillard / Startup.cs
Last active August 4, 2022 09:17
Validating asymmetric jwt with public key
// ... imports
namespace Authentication.WebClient {
public class Startup {
private readonly IConfiguration configuration;
public Startup(IConfiguration configuration) {
this.configuration = configuration;
}
@NMillard
NMillard / GenerateKeys.cs
Last active August 4, 2022 09:17
Create RSA key paris
class Program {
static void Main(string[] args) {
using RSA rsa = RSA.Create();
Console.WriteLine($"-----Private key-----{Environment.NewLine}{Convert.ToBase64String(rsa.ExportRSAPrivateKey())}{Environment.NewLine}");
Console.WriteLine($"-----Public key-----{Environment.NewLine}{Convert.ToBase64String(rsa.ExportRSAPublicKey())}");
}
}
@NMillard
NMillard / appsettings.json
Created March 24, 2020 13:01
Appsettings for JWT example
{
"Jwt": {
"Symmetric": {
"Key": ""
},
"Asymmetric": {
"PrivateKey": "",
"PublicKey": "MIIBCgKCAQEAsXoUruE3QybI3ygaARBUl0e663kxvylbSqlLBPf/lONUpWNucph5RQK/9WepNS/Z42Y79x+jf6MyPkgpMoLiiYB6Nzm5ssmZHg0ImzLmdyc3enCA0/TNrX8QBqieeLmm4Qja2pgsqtX7ae4En2Mr38qLrrpiMOXRqtxgVYqi+Lv9UxfVkRwHB4C+wc9FkM0IhmCja+AvpvtG7UBskPYLRB8o9gELVggKpV9t48yIEtJXG97BzmH3anEYZJY611NylZ1VUbnFbmRGJjAqF6Gy4bcGJbJrRlEQSGZT7mKKlxEijBR1VjADaQc8YVQXI76q1B3NiBcfvPSOJ+MSszd36QIDAQAB"
}
},
@NMillard
NMillard / AuthorizationController.cs
Last active March 24, 2020 12:44
Generate and validate JWT
// ... imports
namespace Authentication.WebClient.Controllers {
[ApiController]
[Route("api/[controller]/[action]")]
public class AuthorizationController : ControllerBase {
private readonly IConfiguration configuration;
public AuthorizationController(IConfiguration configuration) {
@NMillard
NMillard / Startup.cs
Last active November 13, 2020 20:02
Configuring Startup to use JWT authorization
// ... imports
namespace Authentication.WebClient {
public class Startup {
private readonly IConfiguration configuration;
public Startup(IConfiguration configuration) {
this.configuration = configuration;
}
@NMillard
NMillard / ApplicationContext.cs
Last active January 18, 2020 15:34
EntityFramework examples
using EFPractice.DataLayer.Interfaces;
using EFPractice.Domain;
using Microsoft.EntityFrameworkCore;
namespace EFPractice.DataLayer {
internal class ApplicationContext : DbContext, IDataStore {
public ApplicationContext(DbContextOptions options) : base(options) {}
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }