Example #1
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null)
        {
            UserProfile userProfile = new UserProfile();

            UserRoleViewModel userRole = new UserRoleViewModel();

            if (ModelState.IsValid)
            {
                // Obtenga la información sobre el usuario del proveedor de inicio de sesión externo
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    throw new ApplicationException("Error loading external login information during confirmation.");
                }
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.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);

                        #region para la base de datos local para los permisos del menu
                        userProfile.ApplicationUserId = user.Id;
                        userProfile.FirstName         = model.Email;
                        userProfile.LastName          = model.Email;
                        userProfile.Email             = model.Email;
                        userProfile.Password          = "";
                        userProfile.ConfirmPassword   = "";
                        userRole.ApplicationUserId    = user.Id;
                        userRole.CounterId            = 1;      //Codigo para activar el rol
                        userRole.IsHaveAccess         = true;   //Logica para activar el rol
                        userRole.RoleName             = "Menu"; //Contralador para activar el rol
                        await _userManager.AddToRoleAsync(user, userRole.RoleName);

                        _context.UserProfile.Add(userProfile);
                        await _context.SaveChangesAsync();

                        #endregion

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(nameof(ExternalLogin), model));
        }
Example #2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            UserProfile userProfile = new UserProfile();

            UserRoleViewModel userRole = new UserRoleViewModel();

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

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

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation("User created a new account with password.");
                    //return RedirectToLocal(returnUrl);

                    #region para la base de datos local para los permisos del menu
                    userProfile.ApplicationUserId = user.Id;
                    userProfile.FirstName         = model.Nombre;
                    userProfile.LastName          = model.Apellido;
                    userProfile.Email             = model.Email;
                    userProfile.Password          = model.Password;
                    userProfile.ConfirmPassword   = model.Password;
                    userRole.ApplicationUserId    = user.Id;
                    userRole.CounterId            = 1;      //Codigo para activar el rol
                    userRole.IsHaveAccess         = true;   //Logica para activar el rol
                    userRole.RoleName             = "Menu"; //Contralador para activar el rol
                    await _userManager.AddToRoleAsync(user, userRole.RoleName);

                    _context.UserProfile.Add(userProfile);
                    await _context.SaveChangesAsync();

                    #endregion



                    //await Insert(userProfile);
                    return(RedirectToLocalEmail(returnUrl));
                }
                AddErrors(result);
            }


            // Si llegamos tan lejos, algo falló, vuelva a mostrar el formulario
            if (model == null)
            {
                return(View());
            }
            else
            {
                return(View(model));
            }
        }