Ejemplo n.º 1
0
        public async Task OnGetAsync(string returnUrl = null)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                ModelState.AddModelError(string.Empty, ErrorMessage);
            }

            returnUrl ??= Url.Content("~/");

            if (!_userManager.Users.Any())
            {
                string pic = "";
                try
                {
                    IFormFile file = null;
                    pic = await AzureStorageServices.UploadPictureAsync(file);
                }
                catch { }

                var admin = new AppUser
                {
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    FirstName = "Admin",
                    LastName  = "Account",
                    Role      = "Admin",
                    Picture   = pic,
                    inClass   = "False"
                };

                var result = await _userManager.CreateAsync(admin, "BytMig123!");

                if (result.Succeeded)
                {
                    if (!_roleManager.Roles.Any())
                    {
                        await _roleManager.CreateAsync(new IdentityRole("Admin"));

                        await _roleManager.CreateAsync(new IdentityRole("Student"));

                        await _roleManager.CreateAsync(new IdentityRole("Teacher"));
                    }

                    await _userManager.AddToRoleAsync(admin, "Admin");
                }
            }

            await AzureStorageServices.UploadBackgroundAsync();

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            ReturnUrl = returnUrl;
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostChangePictureAsync(IFormFile file)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            string pic = await AzureStorageServices.UploadPictureAsync(file);

            if (pic != user.Picture)
            {
                user.Picture = pic;
                await _userManager.UpdateAsync(user);


                StatusMessage = "Picture changed!";
                return(RedirectToPage());
            }

            StatusMessage = "Your picture is unchanged.";
            return(RedirectToPage());
        }
        public async Task <IActionResult> OnPostAsync(IFormFile file)
        {
            string returnUrl = Url.Content("~/Identity/Account/Register");
            string pic       = "";

            try
            {
                pic = await AzureStorageServices.UploadPictureAsync(file);
            }
            catch { }


            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new AppUser
                {
                    UserName  = Input.Email,
                    Email     = Input.Email,
                    FirstName = Input.FirstName,
                    LastName  = Input.LastName,
                    Role      = Input.Role,
                    Picture   = pic,
                    inClass   = "False"
                };

                var result = await _userManager.CreateAsync(user, Input.Password);

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



                    if (_userManager.Users.Count() == 1 && user.Role == "Admin")
                    {
                        await _userManager.AddToRoleAsync(user, "Admin");
                    }
                    else
                    {
                        if (user.Role == "Student")
                        {
                            await _userManager.AddToRoleAsync(user, "Student");
                        }
                        else
                        {
                            await _userManager.AddToRoleAsync(user, "Teacher");
                        }
                    }


                    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, returnUrl = returnUrl },
                        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, returnUrl = returnUrl }));
                    }
                    else
                    {
                        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());
        }