Exemplo n.º 1
0
        public async Task<ActionResult> Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var blogContext = new BlogContext();
            var user = new User
            {
                Name = model.Name,
                Email = model.Email
            };

            await blogContext.Users.InsertOneAsync(user);
            return RedirectToAction("Index", "Home");
        }
 ///<summary>
 ///Controla el flujo de la aplicacion segun el estado del usuario
 ///</summary>
 ///<param name="user">Obtener los datos del usuario de la base de datos.</param>
 ///<param name="actionName">Nombre de la accion que dispara el evento.</param>
 private string AppFacebookControl(ref User user, string actionName)
 {
     string response = null; //Mostrar la vista del controlador actual por defecto
     string accessToken = (string)Session["AccessToken"];
     if (!string.IsNullOrWhiteSpace(accessToken))
     {
         try
         {
             dynamic me = FBClient(accessToken).Get("me"); //Obtener datos de un Usuario desde Facebook.
             string id = me != null ? me.id : null;//Obtener Id de Facebook, de lo contrario retorna null.
             if (!string.IsNullOrWhiteSpace(id)) //Comprobar el id del usuario.
             {
                 using (var context = new Models.DataContext())
                 {
                     user = context.Users.SingleOrDefault(x => x.IdUser == id); //Obtener al Usuario de la Base de Datos.
                 }
                 if (user != null)
                 {
                     if (actionName != "Ok")
                     {
                         response = "Index"; //El usuario instalo la aplicacion y realizo el registro exitosamente.
                     }
                 }
                 else
                 {
                     if (actionName != "Register")
                     {
                         response = "Register"; //El usuario instalo la aplicacion pero no ha registrado sus datos.
                     }
                 }
                 return response;
             }
         }
         catch
         {
             Session.Remove("AccessToken"); //El token ha expirado
         }
     }
     if (actionName != "Install")
     {
         response = "Install"; //El usuario no ha instalado la aplicacion o el token ha expirado.
     }
     return response;
 }
 public ActionResult NewUser(User user)
 {
     if (!ModelState.IsValid)
     {
         return View("Register", user);
     }
     else
     {
         //Registrar el Usuario en la Base de Datos si previamente no existe
         using (var context = new Models.DataContext())
         {
             if (!context.Users.Any(x => x.IdUser == user.IdUser))
             {
                 context.Users.Add(user);
                 context.SaveChanges();
             }
         }
         return RedirectToAction("Index");
     }
 }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User()
                {
                    UserName = model.UserName,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    Phone = model.Phone,
                    CellPhone = model.CellPhone,
                    Gender = model.Gender,
                    Birthday = model.Birthday,
                    Address = model.Address,
                    CreationDate = DateTime.UtcNow.AddHours(-5)
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // Si llegamos a este punto, es que se ha producido un error y volvemos a mostrar el formulario
            return View(model);
        }
 private async Task SignInAsync(User user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Obtener datos del usuario del proveedor de inicio de sesión externo
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new User() { 
                    UserName = model.UserName,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    Phone = model.Phone,
                    CellPhone = model.CellPhone,
                    Gender = model.Gender,
                    Birthday = model.Birthday,
                    Address = model.Address,
                    CreationDate = DateTime.UtcNow.AddHours(-5)
                };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 7
0
 public string GenerateHashPassword(string password, User user)
 {
     SHA1 sha1 = SHA1.Create();
     string dataToHash = user.Name + password + user.Email;
     byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(dataToHash));
     StringBuilder returnValue = new StringBuilder();
     for (int i = 0; i < hashData.Length; i++)
     {
         returnValue.Append(hashData[i].ToString());
     }
     return returnValue.ToString();
 }