public async Task <IActionResult> EditUser(IFormCollection form)
        {
            bool isAdmin = _userManager.IsInRoleAsync(_userManager.Users.SingleOrDefault(x => x.Id == _userManager.GetUserId(User)), "Administrador").Result;

            if (!isAdmin)
            {
                return(RedirectToAction("AccessDenied", "Home"));
            }
            ContratoDigitalUser user = _userManager.FindByNameAsync(form["UserName"]).Result;

            user.Nombre   = form["Nombre"];
            user.Apellido = form["Apellido"];
            user.Email    = form["Email"];
            await _userManager.RemovePasswordAsync(user);

            await _userManager.AddPasswordAsync(user, form["Password"]);

            var roles = _userManager.GetRolesAsync(user).Result;
            await _userManager.RemoveFromRolesAsync(user, roles);

            await _userManager.AddToRoleAsync(user, form["Rol"]);

            await _userManager.UpdateAsync(user);

            return(RedirectToAction("UserDetails", "Dashboard", new { id = user.UserName }));
        }
Exemple #2
0
        private async Task LoadSharedKeyAndQrCodeUriAsync(ContratoDigitalUser user)
        {
            // Load the authenticator key & QR code URI to display on the form
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            SharedKey = FormatKey(unformattedKey);

            var email = await _userManager.GetEmailAsync(user);

            AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey);
        }
        public IActionResult EditUser(string id)
        {
            bool isAdmin = _userManager.IsInRoleAsync(_userManager.Users.SingleOrDefault(x => x.Id == _userManager.GetUserId(User)), "Administrador").Result;

            if (!isAdmin)
            {
                return(RedirectToAction("AccessDenied", "Home"));
            }
            ContratoDigitalUser user = _userManager.FindByNameAsync(id).Result;

            if (user != null)
            {
                return(View(user));
            }
            else
            {
                ViewData["Error"] = "El usuario no existe";
                return(View(new ContratoDigitalUser()));
            }
        }
Exemple #4
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information during confirmation.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            if (ModelState.IsValid)
            {
                var user = new ContratoDigitalUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            LoginProvider = info.LoginProvider;
            ReturnUrl     = returnUrl;
            return(Page());
        }
Exemple #5
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new ContratoDigitalUser {
                    UserName = Input.Email, Email = Input.Email
                };
                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());
        }
        public async Task <IActionResult> AddUser(IFormCollection form)
        {
            bool isAdmin = _userManager.IsInRoleAsync(_userManager.Users.SingleOrDefault(x => x.Id == _userManager.GetUserId(User)), "Administrador").Result;

            if (!isAdmin)
            {
                return(RedirectToAction("AccessDenied", "Home"));
            }
            var user = new ContratoDigitalUser();

            user.UserName       = form["UserName"];
            user.Email          = form["Email"];
            user.EmailConfirmed = true;

            user.Nombre   = form["Nombre"];
            user.Apellido = form["Apellido"];
            int.TryParse(s: form["Agencia"], result:  out int agencia);
            user.Cedula = form["Cedula"];
            string userPwd = form["Password"];

            user.Agencia            = agencia;
            user.DescripcionAgencia = form["DescripcionAgencia"];

            WebserviceController service = new WebserviceController(_context, _emailConfiguration, _hostingEnvironment, _utilities, _userManager, _canonicalUrlConfiguration);
            string resultSiicon          = service.GetSiiconUserId(user.Cedula).Result.Value;
            string resultAsesor          = service.GetAsesorId(user.Cedula).Result.Value;

            string first  = resultSiicon;
            string second = resultAsesor;

            if (form["Rol"].Contains("Asesor"))
            {
                /*if (!String.IsNullOrEmpty(resultSiicon) && resultSiicon != "[]")
                 * {
                 *  dynamic jsonSiicon = JsonConvert.DeserializeObject<dynamic>(resultSiicon);
                 *  user.IdSiicon = jsonSiicon.First.TerceroId;
                 * }
                 * else
                 * {
                 *  return RedirectToAction("Users", "Dashboard", new { e = (int)Constants.ErrorList.NoPoseeTerceroID });
                 * }*/
                if (!String.IsNullOrEmpty(resultAsesor) && resultAsesor != "[]")
                {
                    dynamic jsonAsesor = JsonConvert.DeserializeObject <dynamic>(resultAsesor);
                    user.Asesor   = jsonAsesor.First.CodAsesor;
                    user.IdSiicon = jsonAsesor.First.TerceroId;
                }
                else
                {
                    return(RedirectToAction("Users", "Dashboard", new { e = (int)Constants.ErrorList.NoPoseeTerceroID }));
                }

                if (user.Asesor.Equals(0) && String.IsNullOrEmpty(user.IdSiicon))
                {
                    return(RedirectToAction("Users", "Dashboard", new { e = (int)Constants.ErrorList.NoPoseeIDSiicon }));
                    //return View(user);
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(resultSiicon) && resultSiicon != "[]")
                {
                    dynamic jsonSiicon = JsonConvert.DeserializeObject <dynamic>(resultSiicon);
                    user.IdSiicon = jsonSiicon.First.TerceroId;
                    user.Asesor   = 0;
                }
                else
                {
                    return(RedirectToAction("Users", "Dashboard", new { e = (int)Constants.ErrorList.NoPoseeTerceroID }));
                }


                if (String.IsNullOrEmpty(user.IdSiicon))
                {
                    return(RedirectToAction("Users", "Dashboard", new { e = (int)Constants.ErrorList.NoPoseeIDSiicon }));
                    //return View(user);
                }
            }


            IdentityResult chkUser = await _userManager.CreateAsync(user, userPwd);

            if (chkUser.Succeeded)
            {
                var result = await _userManager.AddToRoleAsync(user, form["Rol"]);
            }
            return(RedirectToAction("UserDetails", "Dashboard", new { id = user.UserName }));
        }