You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Dotnet-WebAPI/AppContext/AppContext.cs

45 lines
1.3 KiB

using System.Security.Cryptography;
using AppContext.Entities;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using Microsoft.EntityFrameworkCore;
namespace AppContext;
public class AppContext(DbContextOptions<AppContext> options) : DbContext(options)
{
public DbSet<UserEntity> Users { get; }
public DbSet<TacticEntity> Tactics { get; }
public DbSet<TeamEntity> Teams { get; }
public DbSet<MemberEntity> Members { get; }
public AppContext() : this(
new DbContextOptionsBuilder<AppContext>()
.UseSqlite("Data Source=database.db")
.Options
)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<UserEntity>()
.Property(e => e.Password)
.HasConversion(
v => HashString(v),
v => v
);
}
private static string HashString(string str)
{
byte[] salt = RandomNumberGenerator.GetBytes(128 / 8);
return Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: str,
salt,
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 50000,
numBytesRequested: 256 / 8
));
}
}