public async Task <(bool success, string message)> TryLoginAsync(string username, string password) { var dbUser = _db.Users.FirstOrDefault(u => u.Username == username); if (dbUser is null) { return(false, "Unknown username or wrong password."); } var passwordHash = _crypt.GenerateHash(dbUser.Salt, password); if (!_isDevelopment && passwordHash != dbUser.PasswordHash) { return(false, "Unknown username or wrong password."); } var role = dbUser.Usertype.ToString(); var claims = new List <Claim> { new Claim(ClaimTypes.Name, username), new Claim(ClaimTypes.Role, role), //new Claim("Userdata", JsonSerializer.Serialize(currentUser)), }; var claimsIdentity = new ClaimsIdentity( claims, Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddHours(3), }; await HttpContext.SignInAsync( Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); return(true, string.Empty); }
public void Seed(ICryptService cryptService) { Randomizer.Seed = new Random(1938); var faker = new Faker("de"); var adminSalt = cryptService.GenerateSecret(256); var admin = new User( username: "******", salt: adminSalt, passwordHash: cryptService.GenerateHash(adminSalt, "1234"), usertype: Usertype.Admin); Users.Add(admin); SaveChanges(); var i = 0; var stores = new Faker <Store>("de").CustomInstantiator(f => { var name = f.Company.CompanyName(); var salt = cryptService.GenerateSecret(256); var username = $"store{++i:000}"; return(new Store( name: f.Company.CompanyName(), closeDate: new DateTime(2021, 1, 1).AddDays(f.Random.Int(0, 365)).OrNull(f, 0.5f), url: f.Internet.Url().OrDefault(f, 0.25f), manager: new User( username: username, salt: salt, passwordHash: cryptService.GenerateHash(salt, "1234"), usertype: Usertype.Owner))); }) .Generate(10) .GroupBy(s => s.Name).Select(g => g.First()) .ToList(); Stores.AddRange(stores); SaveChanges(); var productCategories = new Faker <ProductCategory>("de").CustomInstantiator(f => { return(new ProductCategory( name: f.Commerce.ProductAdjective(), nameEn: f.Commerce.ProductAdjective().OrDefault(f, 0.5f))); }) .Generate(10) .GroupBy(p => p.Name).Select(g => g.First()) .ToList(); ProductCategories.AddRange(productCategories); SaveChanges(); var products = new Faker <Product>("de").CustomInstantiator(f => { return(new Product( ean: f.Random.Int(100000, 999999), name: f.Commerce.ProductName(), productCategory: f.Random.ListItem(productCategories), recommendedPrice: (f.Random.Int(1000, 100000) / 100M).OrNull(f, 0.3f), availableFrom: new DateTime(2022, 1, 1).AddDays(f.Random.Int(0, 300)).OrNull(f, 0.8f) )); }) .Generate(100) .GroupBy(p => p.Ean).Select(g => g.First()) .ToList(); Products.AddRange(products); SaveChanges(); var offers = new Faker <Offer>("de").CustomInstantiator(f => { var store = f.Random.ListItem(stores); var lastUpdate = (store.CloseDate ?? new DateTime(2022, 1, 1)).AddDays(-f.Random.Int(0, 365)); return(new Offer( product: f.Random.ListItem(products), store: store, price: f.Random.Int(1000, 99900) / 100M, soldOut: f.Random.Bool(0.25f), lastUpdate: lastUpdate)); }) .Generate(500) .GroupBy(o => new { o.StoreId, o.ProductEan }).Select(g => g.First()) .ToList(); Offers.AddRange(offers); SaveChanges(); // Tägliche Preise generieren. Dabei wird eine Schwankung von bis zu 5 % // alle 20 - 40 Tage in die Tabelle priceTrends geschrieben. var priceTrends = new List <PriceTrend>(); foreach (var offer in offers) { var start = new DateTime(2021, 12, 31); var end = new DateTime(2021, 1, 1); var priceCent = (int)(offer.Price * 100); for (var date = start; date >= end; date = date.AddDays(-faker.Random.Int(20, 40))) { priceTrends.Add(new PriceTrend( offer: offer, date: date, price: priceCent / 100M)); var diff = (int)(priceCent * 0.05); priceCent = faker.Random.Int(priceCent - diff, priceCent + diff); } } PriceTrends.AddRange(priceTrends); SaveChanges(); }
public void Seed(ICryptService cryptService) { Randomizer.Seed = new Random(1938); var adminSalt = cryptService.GenerateSecret(256); var admin = new User( username: "******", salt: adminSalt, passwordHash: cryptService.GenerateHash(adminSalt, "1234"), usertype: Usertype.Admin); Users.Add(admin); SaveChanges(); var i = 0; var stores = new Faker <Store>("de").CustomInstantiator(f => { var name = f.Company.CompanyName(); var salt = cryptService.GenerateSecret(256); var username = $"store{++i:000}"; return(new Store( name: f.Company.CompanyName(), closeDate: new DateTime(2021, 1, 1).AddDays(f.Random.Int(0, 365)).OrNull(f, 0.5f), url: f.Internet.Url().OrDefault(f, 0.25f), manager: new User( username: username, salt: salt, passwordHash: cryptService.GenerateHash(salt, "1234"), usertype: Usertype.Owner))); }) .Generate(10) .GroupBy(s => s.Name).Select(g => g.First()) .ToList(); Stores.AddRange(stores); SaveChanges(); var productCategories = new Faker <ProductCategory>("de").CustomInstantiator(f => { return(new ProductCategory( name: f.Commerce.ProductAdjective(), nameEn: f.Commerce.ProductAdjective().OrDefault(f, 0.5f))); }) .Generate(10) .GroupBy(p => p.Name).Select(g => g.First()) .ToList(); ProductCategories.AddRange(productCategories); SaveChanges(); var products = new Faker <Product>("de").CustomInstantiator(f => { return(new Product( ean: f.Random.Int(100000, 999999), name: f.Commerce.ProductName(), productCategory: f.Random.ListItem(productCategories), recommendedPrice: (f.Random.Int(1000, 100000) / 100M).OrNull(f, 0.3f), availableFrom: new DateTime(2022, 1, 1).AddDays(f.Random.Int(0, 300)).OrNull(f, 0.8f) )); }) .Generate(100) .GroupBy(p => p.Ean).Select(g => g.First()) .ToList(); Products.AddRange(products); SaveChanges(); var offers = new Faker <Offer>("de").CustomInstantiator(f => { var store = f.Random.ListItem(stores); var lastUpdate = (store.CloseDate ?? new DateTime(2022, 1, 1)).AddDays(-f.Random.Int(0, 365)); return(new Offer( product: f.Random.ListItem(products), store: store, price: f.Random.Int(1000, 99900) / 100M, soldOut: f.Random.Bool(0.25f), lastUpdate: lastUpdate)); }) .Generate(500) .GroupBy(o => new { o.StoreId, o.ProductEan }).Select(g => g.First()) .ToList(); Offers.AddRange(offers); SaveChanges(); }
public void Seed(ICryptService crypt) { Randomizer.Seed = new Random(1035); var faker = new Faker("de"); var teachers = new Faker <Teacher>("de") .CustomInstantiator(f => { var lastname = f.Name.LastName(); var shortname = lastname.Left(3).ToUpper(); return(new Teacher( shortname: shortname, firstname: f.Name.FirstName(), lastname: lastname, email: f.Internet.Email())); }) .Generate(50) .GroupBy(t => t.Shortname) .Select(g => g.First()) .Take(30) .ToList(); Teachers.AddRange(teachers); SaveChanges(); var users = new Faker <User>("de") .CustomInstantiator(f => { var teacher = f.Random.ListItem(teachers); var salt = crypt.GenerateSecret(256); return(new User( username: teacher.Email, salt: salt, passwordHash: crypt.GenerateHash(salt, "1234"), teacher: teacher)); }) .Generate(20) .GroupBy(u => u.TeacherId).Select(g => g.First()) .ToList(); Users.AddRange(users); SaveChanges(); var subjects = new Faker <Subject>("de") .CustomInstantiator(f => { var name = f.Commerce.ProductMaterial(); var shortname = name.Left(3).ToUpper(); return(new Subject( shortname: shortname, name: name)); }) .Generate(50) .GroupBy(t => t.Shortname) .Select(g => g.First()) .Take(10) .ToList(); Subjects.AddRange(subjects); SaveChanges(); var departments = new string[] { "HIF", "HBGM", "HMNA" }; var classes = new Faker <SchoolClass>("de") .CustomInstantiator(f => { var name = $"{f.Random.Int(1, 5)}{f.Random.String2(1, "ABCDE")}{f.Random.ListItem(departments)}"; return(new SchoolClass(name, f.Random.ListItem(teachers))); }) .Rules((f, sc) => { var room = $"{f.Random.String2(1, "ABCD")}{f.Random.String2(1, "E1234")}.{f.Random.String2(1, "01")}{f.Random.Int(1, 9)}"; sc.Room = room.OrDefault(f, 0.2f); int i = 100; var students = new Faker <Student>("de") .CustomInstantiator(f2 => { var lastname = f2.Name.LastName(); var firstname = f2.Name.FirstName(); var account = $"{(lastname.Length < 3 ? lastname : lastname.Substring(0, 3)).ToUpper()}{i:0000}"; var dateOfBirth = new DateTime(2000, 1, 1) .AddDays(f2.Random.Int(0, 5 * 365)); i++; var home = new Address( f2.Address.City(), f2.Random.Int(1000, 9999).ToString(), f2.Address.StreetAddress()); return(new Student( account: account, lastname: lastname, firstname: firstname, home: home, dateOfBirth: dateOfBirth, schoolClass: sc)); }) .Rules((f2, s) => { s.Email = f2.Internet.Email(); var parents = new Address( f2.Address.City(), f2.Random.Int(1000, 9999).ToString(), f2.Address.StreetAddress()); s.Parents = parents.OrDefault(f2, 0.25f); }) .Generate(20) .ToList(); sc.Students.AddRange(students); }) .Generate(20) .GroupBy(s => s.Name) .Select(g => g.First()) .Take(10) .ToList(); SchoolClasses.AddRange(classes); SaveChanges(); // 20 exams per class -> 200 exams var exams = new Faker <Exam>("de") .CustomInstantiator(f => { return(new Exam( teacher: f.Random.ListItem(teachers), subject: f.Random.ListItem(subjects), date: new DateTime(2021, 10, 1) .AddDays(f.Random.Int(0, 30 * 9)) .AddHours(f.Random.Int(8, 16)), schoolClass: f.Random.ListItem(classes))); }) .Generate(200) .ToList(); Exam.AddRange(exams.Take(100)); SaveChanges(); CommitedExams.AddRange(exams .Skip(100) .Select(e => { var room = $"{faker.Random.String2(1, "ABCD")}{faker.Random.String2(1, "E1234")}.{faker.Random.String2(1, "01")}{faker.Random.Int(1, 9)}"; return(new CommitedExam(e, room)); })); SaveChanges(); }