This repository has been archived on 2023-02-13. You can view files and clone it, but cannot push or open issues or pull requests.
gswi-server/gswi/DataSeeder.cs

77 lines
2.4 KiB
C#
Raw Normal View History

2022-02-20 19:04:11 +01:00
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Microsoft.EntityFrameworkCore;
using gswi.Data;
using gswi.Model;
namespace gswi
{
public class DataSeeder
{
private readonly DatabaseContext _databaseContext;
public DataSeeder(DatabaseContext databaseContext)
{
_databaseContext = databaseContext;
}
public void SeedData()
{
_databaseContext.Database.EnsureCreated();
if (!_databaseContext.AuthUsers.Any())
{
var admin = new AuthUser()
{
FirstName = "Admin",
LastName = "Administator",
EMail = "admin@localhost",
Password = ComputeHash("Administator", new SHA256CryptoServiceProvider()),
AuthRole = AuthRoles.Admin
};
var authUser = new AuthUser()
{
FirstName = "Max",
LastName = "Mustermann",
EMail = "max.mustermann@gmail.com",
Password = ComputeHash("test1234", new SHA256CryptoServiceProvider()),
};
var authUser1 = new AuthUser()
{
FirstName = "Max",
LastName = "Tester",
EMail = "max.mustermann@mustermail.com",
Password = ComputeHash("test1234", new SHA256CryptoServiceProvider()),
};
var authUser2 = new AuthUser()
{
FirstName = "Max",
LastName = "Muster",
EMail = "max.mustermann@yahoo.com",
Password = ComputeHash("test1234", new SHA256CryptoServiceProvider()),
};
_databaseContext.AuthUsers.Add(admin);
_databaseContext.AuthUsers.Add(authUser);
_databaseContext.AuthUsers.Add(authUser1);
_databaseContext.AuthUsers.Add(authUser2);
}
_databaseContext.SaveChanges();
}
public string ComputeHash(string input, HashAlgorithm algorithm)
{
Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
return BitConverter.ToString(hashedBytes);
}
}
}