public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            // Something failed, redisplay form
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // Check if the user exists in the data store
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user != null)
            {
                var password = "******" + GenerateNumber.Generate().ToString();
                await _userManager.RemovePasswordAsync(user);

                await _userManager.AddPasswordAsync(user, password);

                user.EmailConfirmed = false;
                await _userManager.UpdateAsync(user);

                var listadoEmails = new List <string>();
                listadoEmails.Add(user.Email);
                var cuerpo = emailSender.CuerpoCreateUser(Configuration.GetSection("ResetearContrasenaCorreo").Value, string.Format("{0} {1}", user.Name, user.LastName),
                                                          user.Email, password, Configuration.GetSection("EmailLink").Value);
                emailSender.SendEmailAsync(listadoEmails, Mensaje.AsuntoCorreo, cuerpo);


                return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
            }
            ModelState.AddModelError(string.Empty, "El correo electrónico ingresado no existe.");
            return(View(model));
        }
示例#2
0
        private static void  Example()
        {
            GenerateNumber.Lower = 10;
            GenerateNumber.Top   = 25;
            Console.WriteLine(GenerateNumber.Generate().ToString());

            Console.WriteLine(GenerateNumber.Generate(10, 66).ToString());
            Console.ReadLine();
            Example();
        }
示例#3
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            // Something failed, redisplay form
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // Check if the user exists in the data store
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user != null)
            {
                var password = PasswordUtil.Password + GenerateNumber.Generate().ToString();
                await _userManager.RemovePasswordAsync(user);

                await _userManager.AddPasswordAsync(user, password);

                user.EmailConfirmed = false;
                await _userManager.UpdateAsync(user);

                var mensaje = "Recuperar contraseña"
                              + "\n \n Hola Señor(a): " + user.Name + " " + user.LastName
                              + "\n \n Le informamos que se ha reseteado su contraseña."
                              + "\n \n Nuevas credenciales de ingreso al sistema."
                              + "\n \n Usuario:  " + user.Email
                              + "\n \n Contraseña temporal: " + password
                              + "\n \n Click en el siguiente enlace para acceder al sistema" + "\n \n"
                              + Configuration.GetSection("EmailLink").Value
                              + Configuration.GetSection("EmailFooter").Value;

                Mail mail = new Mail
                {
                    Body = mensaje
                    ,
                    EmailTo = user.Email
                    ,
                    NameTo = "Name To"
                    ,
                    Subject = "Recuperar contraseña",
                };

                //execute the method Send Mail or SendMailAsync
                var a = await Emails.SendEmailAsync(mail);

                return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
            }
            ModelState.AddModelError(string.Empty, "El correo electrónico ingresado no existe.");
            return(View(model));
        }
 public async Task <IActionResult> Post([FromBody] GenerateNumber generateNumberModel)
 {
     if (!ModelState.IsValid)
     {
         _logger.Log(LogLevel.Error, $"Error {ModelState}");
         return(BadRequest(ModelState));
     }
     _logger.Log(LogLevel.Information, $"Generator service received a request. order id: '{generateNumberModel.OrderId}', Batch count :'{generateNumberModel.BatchCount}'.");
     try
     {
         _generatorService.GenerateRandomNumber(_cancellationService.GetToken(), generateNumberModel.OrderId, generateNumberModel.BatchCount);
     }
     catch (OperationCanceledException)
     {
         _logger.Log(LogLevel.Information, $"All services are canceled.");
     }
     return(Accepted());
 }
        public async Task <IActionResult> Manage(RegisterViewModel user)
        {
            try
            {
                ViewBag.accion = string.IsNullOrEmpty(user.Id) == true ? "Crear" : "Editar";
                if (ModelState.IsValid)
                {
                    var existeRegistro = false;
                    if (string.IsNullOrEmpty(user.Id) == true)
                    {
                        if (!await db.Users.AnyAsync(c => c.UserName.ToUpper().Trim() == user.Email.ToUpper().Trim()))
                        {
                            var RegistredUser = new ApplicationUser
                            {
                                Name           = user.Name,
                                LastName       = user.LastName,
                                Address        = user.Address,
                                PhoneNumber    = user.PhoneNumber,
                                UserName       = user.Email,
                                Email          = user.Email,
                                EmailConfirmed = false,
                                Status         = user.Status,
                            };
                            var password = "******" + GenerateNumber.Generate().ToString();
                            var z        = await userManager.CreateAsync(RegistredUser, password);

                            var userd = await userManager.FindByEmailAsync(user.Email);

                            if (!await userManager.IsInRoleAsync(userd, user.IdRol))
                            {
                                await userManager.AddToRoleAsync(userd, user.IdRol);
                            }



                            var listadoEmails = new List <string>();
                            listadoEmails.Add(user.Email);
                            var cuerpo = emailSender.CuerpoCreateUser(Configuration.GetSection("CreateUsuarioCorreo").Value, string.Format("{0} {1}", userd.Name, userd.LastName),
                                                                      userd.Email, password, Configuration.GetSection("EmailLink").Value);
                            emailSender.SendEmailAsync(listadoEmails, Mensaje.AsuntoCorreo, cuerpo);
                        }

                        else
                        {
                            existeRegistro = true;
                        }
                    }
                    else
                    {
                        if (!await db.Users.Where(c => c.UserName.ToUpper().Trim() == user.Email.ToUpper().Trim()).AnyAsync(c => c.Id != user.Id))
                        {
                            var CurrentUser = await userManager.FindByIdAsync(user.Id);

                            CurrentUser.LastName    = user.LastName;
                            CurrentUser.Name        = user.Name;
                            CurrentUser.Address     = user.Address;
                            CurrentUser.PhoneNumber = user.PhoneNumber;
                            CurrentUser.UserName    = user.Email;
                            CurrentUser.Email       = user.Email;
                            CurrentUser.Status      = user.Status;

                            var RolsUser = await db.UserRoles.Where(x => x.UserId == user.Id).ToListAsync();

                            db.UserRoles.RemoveRange(RolsUser);
                            await db.SaveChangesAsync();

                            if (!await userManager.IsInRoleAsync(CurrentUser, user.IdRol))
                            {
                                await userManager.AddToRoleAsync(CurrentUser, user.IdRol);
                            }
                        }
                        else
                        {
                            existeRegistro = true;
                        }
                    }
                    if (!existeRegistro)
                    {
                        await db.SaveChangesAsync();

                        return(this.Redireccionar($"{Mensaje.MensajeSatisfactorio}|{Mensaje.Satisfactorio}"));
                    }
                    else
                    {
                        this.TempData["Mensaje"] = $"{Mensaje.Error}|{Mensaje.ExisteRegistro}";
                    }
                    ViewData["IdRol"] = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await db.Roles.ToListAsync(), "Name", "Name", user.IdRol);
                    return(View(user));
                }
                this.TempData["Mensaje"] = $"{Mensaje.Error}|{Mensaje.CorregirFormulario}";
                ViewData["IdRol"]        = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await db.Roles.ToListAsync(), "Name", "Name", user.IdRol);
                return(View(user));
            }
            catch (Exception)
            {
                return(this.Redireccionar($"{Mensaje.Error}|{Mensaje.Excepcion}"));
            }
        }
示例#6
0
        public async Task <IActionResult> Manage(RegisterViewModel user)
        {
            try
            {
                ViewBag.accion = string.IsNullOrEmpty(user.Id) == true ? "Crear" : "Editar";
                if (ModelState.IsValid)
                {
                    var existeRegistro = false;
                    if (string.IsNullOrEmpty(user.Id) == true)
                    {
                        if (!await db.Users.AnyAsync(c => c.UserName.ToUpper().Trim() == user.Email.ToUpper().Trim()))
                        {
                            var RegistredUser = new ApplicationUser
                            {
                                Name           = user.Name,
                                LastName       = user.LastName,
                                Address        = user.Address,
                                PhoneNumber    = user.PhoneNumber,
                                UserName       = user.Email,
                                Email          = user.Email,
                                EmailConfirmed = false,
                                Status         = user.Status,
                            };
                            var password = PasswordUtil.Password + GenerateNumber.Generate().ToString();
                            var z        = await userManager.CreateAsync(RegistredUser, password);

                            var userd = await userManager.FindByEmailAsync(user.Email);

                            if (!await userManager.IsInRoleAsync(userd, user.IdRol))
                            {
                                await userManager.AddToRoleAsync(userd, user.IdRol);
                            }

                            var mensaje = Configuration.GetSection("SubjectCreateUser").Value
                                          + "\n \n Hola Señor(a): " + userd.Name + " " + userd.LastName
                                          + "\n \n Le informamos que se ha creado su usuario en el sistema de seguimiento."
                                          + "\n \n Credenciales de ingreso al sistema."
                                          + "\n \n Usuario:  " + userd.Email
                                          + "\n \n Contraseña temporal: " + password
                                          + "\n \n Click en el siguiente enlace para acceder al sistema" + "\n \n"
                                          + Configuration.GetSection("EmailLink").Value
                                          + Configuration.GetSection("EmailFooter").Value;

                            Mail mail = new Mail
                            {
                                Body = mensaje
                                ,
                                EmailTo = user.Email
                                ,
                                NameTo = "Name To"
                                ,
                                Subject = Configuration.GetSection("SubjectCreateUser").Value,
                            };

                            //execute the method Send Mail or SendMailAsync
                            var a = await Emails.SendEmailAsync(mail);
                        }

                        else
                        {
                            existeRegistro = true;
                        }
                    }
                    else
                    {
                        if (!await db.Users.Where(c => c.UserName.ToUpper().Trim() == user.Email.ToUpper().Trim()).AnyAsync(c => c.Id != user.Id))
                        {
                            var CurrentUser = await userManager.FindByIdAsync(user.Id);

                            CurrentUser.LastName    = user.LastName;
                            CurrentUser.Name        = user.Name;
                            CurrentUser.Address     = user.Address;
                            CurrentUser.PhoneNumber = user.PhoneNumber;
                            CurrentUser.UserName    = user.Email;
                            CurrentUser.Email       = user.Email;
                            CurrentUser.Status      = user.Status;

                            var RolsUser = await db.UserRoles.Where(x => x.UserId == user.Id).ToListAsync();

                            db.UserRoles.RemoveRange(RolsUser);
                            await db.SaveChangesAsync();

                            if (!await userManager.IsInRoleAsync(CurrentUser, user.IdRol))
                            {
                                await userManager.AddToRoleAsync(CurrentUser, user.IdRol);
                            }
                        }
                        else
                        {
                            existeRegistro = true;
                        }
                    }
                    if (!existeRegistro)
                    {
                        await db.SaveChangesAsync();

                        return(this.Redireccionar($"{Mensaje.MensajeSatisfactorio}|{Mensaje.Satisfactorio}"));
                    }
                    else
                    {
                        this.TempData["Mensaje"] = $"{Mensaje.Error}|{Mensaje.ExisteRegistro}";
                    }
                    ViewData["IdRol"] = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await db.Roles.ToListAsync(), "Name", "Name", user.IdRol);
                    return(View(user));
                }
                this.TempData["Mensaje"] = $"{Mensaje.Error}|{Mensaje.CorregirFormulario}";
                ViewData["IdRol"]        = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await db.Roles.ToListAsync(), "Name", "Name", user.IdRol);
                return(View(user));
            }
            catch (Exception ex)
            {
                return(this.Redireccionar($"{Mensaje.Error}|{Mensaje.Excepcion}"));
            }
        }