//Contractor that loads initialies all the private fileds above public CasesController(ApplicationDbContext _context) { context = _context; caseOps = new CaseOps(context); offender = new OffenderOps(context); offence = new CrudOperations <Offence>(context); officer = new OfficerOps(context); caseof = new CaseOffenderOps(context); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { //If you have no roles in your database, uncomment the bellow 3 lines of code add superuser on the code in line 107, run the application and capture the first user that will be super admin then //put back officer in the code in line 107 so that the next user will be captured as officer role //await _roleManager.CreateAsync(new IdentityRole { Name = "SuperAdmin" }); //await _roleManager.CreateAsync(new IdentityRole { Name = "Officer" }); //await _roleManager.CreateAsync(new IdentityRole { Name = "Relative" }); //add the newly created username to the officer instance officer.User = user; //create an instance of the class that implements Officer CRUD operations OfficerOps officerOps = new OfficerOps(_context); //Insert officer officerOps.Insert(officer); //Add the new officer to the SuperAdmin roles await _userManager.AddToRoleAsync(user, "SuperAdmin"); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public OfficerController(ApplicationDbContext _context) { context = _context; officer = new OfficerOps(context); }