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

52 lines
1.5 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; init; }
public DbSet<TacticEntity> Tactics { get; init; }
public DbSet<TeamEntity> Teams { get; init; }
public DbSet<MemberEntity> Members { get; init; }
public DbSet<TacticStepEntity> TacticSteps { get; set; }
public AppContext() : this(
new DbContextOptionsBuilder<AppContext>()
.UseSqlite("DataSource=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
);
builder.Entity<MemberEntity>()
.HasKey("UserId", "TeamId");
builder.Entity<TacticStepEntity>()
.HasKey("TacticId", "StepId");
}
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
));
}
}