Example #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Petitioner pUser = await _userManager.GetUserAsync(User);

            var filename = Guid.NewGuid().ToString() + Path.GetExtension(PetitionPicture.FileName);
            var file     = Path.Combine(_environment.ContentRootPath, "wwwroot", "petition-photos", filename);

            using (var fileStream = new FileStream(file, FileMode.Create))
            {
                await PetitionPicture.CopyToAsync(fileStream);
            }
            InputPetitionModel.Photo           = filename;
            InputPetitionModel.PetitionModelId = Guid.NewGuid().ToString();
            InputPetitionModel.DateCreated     = DateTime.Now;
            InputPetitionModel.Status          = "In Progress";
            InputPetitionModel.Votes           = 1;
            InputPetitionModel.Creator         = pUser;

            _context.PetitionModel.Add(InputPetitionModel);
            await _context.SaveChangesAsync();

            return(RedirectToPage("/Index"));
        }
Example #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                if (Input.Region == null)
                {
                    Input.Region = "";
                }
                var user = new Petitioner {
                    UserName = Input.Email, Email = Input.Email, ResidentialAddress = Input.Address, NRIC = Input.NRIC, ProfilePicture = "default_pic.png", FullName = Input.FullName, Region = Input.Region
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Page(
                    //    "/Account/ConfirmEmail",
                    //    pageHandler: null,
                    //    values: new { 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>.");

                    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());
        }