Ejemplo n.º 1
0
        public async Task <IActionResult> CreateRole(createRoleviewModel vm)
        {
            if (ModelState.IsValid)
            {
                Applicationroles Applicationrole = new Applicationroles()
                {
                    Name = vm.RoleName
                };
                IdentityResult result = await _roleManager.CreateAsync(Applicationrole);

                if (result.Succeeded)
                {
                    return(RedirectToAction("ListRoles", "Admin"));
                }
                //to check if role already exists show error msg already taken
                foreach (IdentityError error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(View(vm));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new Users {
                    UserName = Input.Email, Email = Input.Email, Gender = Input.Gender, Birthdate = Input.Birthdate, MyUsername = Input.MyUsername, SelectedRole = "user"
                };


                if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                {
                    user.SelectedRole = Input.RoleName;
                    await _userManager.CreateAsync(user, Input.Password);

                    await _userManager.AddToRoleAsync(user, Input.RoleName);


                    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));
                    }
                }
                else
                {
                    var result = await _userManager.CreateAsync(user, Input.Password);

                    if (result.Succeeded)
                    {
                        //tempAdmin

                        if (!await _roleManager.RoleExistsAsync("Admin"))
                        {
                            var rolee = new Applicationroles("Admin");
                            await _roleManager.CreateAsync(rolee);

                            await _userManager.AddToRoleAsync(user, "Admin");

                            user.SelectedRole = "Admin";
                            await _userManager.UpdateAsync(user);
                        }
                        else if (!await _roleManager.RoleExistsAsync("User"))
                        {
                            Applicationroles role = new Applicationroles {
                                Name = "User"
                            };
                            await _roleManager.CreateAsync(role);

                            await _userManager.AddToRoleAsync(user, "User");
                        }
                        else
                        {
                            await _userManager.AddToRoleAsync(user, "User");
                        }


                        /*-------------------------------------------------------------*/

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