// Sets up the first user of the forum public async Task <IActionResult> SendSetup(string email, string username, string password, string confirmPassword) { // Redirects if users have already signed up or input is invalid if (_context.Users.Count() != 0) { return(Redirect("/")); } if (email == null || username == null || password == null) { return(RedirectToAction("Setup", new { error = 0 })); } if (password != confirmPassword) { return(RedirectToAction("Setup", new { error = 1 })); } User user = new User() { Username = username, Email = email, Password = password, Role = "Admin", Activated = true }; User addedUser = await _repository.AddUserAsync(user); // Saves changes await _repository.SaveChangesAsync(); // Signs in user ClaimsPrincipal principal = Auth.CreateClaims(user); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties { ExpiresUtc = DateTime.UtcNow.AddMonths(1), IsPersistent = true, AllowRefresh = false }); return(Redirect("/")); }
public RepositoryTest() { // Creates ApplicationDbContext from in memory database DbContextOptions <ApplicationDbContext> options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseSqlite(CreateInMemory()).Options; _connection = RelationalOptionsExtension.Extract(options).Connection; ApplicationDbContext context = new ApplicationDbContext(options); context.Database.EnsureCreated(); // Creates repository and adds data repository = new SimpleForumRepository(context, null, null, null); repository.AddUserAsync(new User() { Username = "******", Password = "******", Email = "*****@*****.**" }).Wait(); repository.SaveChangesAsync().Wait(); repository.AddThreadAsync(new Thread() { Title = "first thread", Content = "Thread content", UserID = 1 }).Wait(); repository.SaveChangesAsync().Wait(); }