Ejemplo n.º 1
0
        public async Task <ActionResult> ViewProfile(ExtendedProfileModels model)
        {
            if (ModelState.IsValid)
            {
                using (IdentityContext idDb = new IdentityContext())
                {
                    var currentUserId = User.Identity.GetUserId();
                    var currentUser   = idDb.Users.FirstOrDefault(x => x.Id == currentUserId);


                    var copy = Clone(currentUser);
                    copy.FirstName = model.FirstName;
                    copy.LastName  = model.LastName;
                    copy.Email     = model.Email;
                    //if (model.Password != null)
                    //{

                    //    copy.PasswordHash = new UserManager<ApplicationUser>(idDb).PasswordHasher.HashPassword(model.Password);
                    //}


                    if (model.UserProfilePicture != null)
                    {
                        if (model.UserProfilePicture.ContentLength > (4 * 1024 * 1024))
                        {
                            ModelState.AddModelError("CustomError", "Image can not be lager than 4MB.");
                            return(View());
                        }
                        if (
                            !(model.UserProfilePicture.ContentType == "image/jpeg" ||
                              model.UserProfilePicture.ContentType == "image/gif"))
                        {
                            ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format.");
                        }

                        byte[] data = new byte[model.UserProfilePicture.ContentLength];
                        model.UserProfilePicture.InputStream.Read(data, 0, model.UserProfilePicture.ContentLength);

                        copy.ProfilePicture = data;
                    }

                    idDb.Entry(currentUser).CurrentValues.SetValues(copy);
                    await idDb.SaveChangesAsync();
                }

                return(RedirectToAction("ViewProfile", "Profile"));
            }
            // If we got this far, something failed, redisplay form
            return(View());
        }
Ejemplo n.º 2
0
        public ActionResult ViewProfile()
        {
            ViewBag.Message = "Your Profile";


            using (IdentityContext _idDb = new IdentityContext())
            {
                var _currentUserId = User.Identity.GetUserId();
                var _currentUser   = _idDb.Users.FirstOrDefault(x => x.Id == _currentUserId);

                var currentUserExtendedIdentity = new ExtendedProfileModels()
                {
                    FirstName       = _currentUser.FirstName,
                    LastName        = _currentUser.LastName,
                    Email           = _currentUser.Email,
                    Image           = _currentUser.ProfilePicture,
                    Password        = _currentUser.PasswordHash,
                    ConfirmPassword = _currentUser.PasswordHash
                };
                return(View(currentUserExtendedIdentity));
            }
        }