Пример #1
0
        public HttpResponseMessage UpdateUser(ManagerUserViewModel user) //cap nhat user
        {
            try
            {
                //update user at aspNetUser Table
                var ApplicationUserUpdate = new ApplicationUser
                {
                    Id = user.UserId,
                    EmailConfirmed = false,
                    PasswordHash = UserManager.PasswordHasher.HashPassword(user.Password),
                    SecurityStamp = user.SecurityStamp,
                    UserName = user.UserName,
                    Email = user.Email,
                };
                context.Entry(ApplicationUserUpdate).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();

                //update userinfo at userinfo table
                UserInfo userInfoModel = new UserInfo();
                //check avatar
                if (user.UserInfo.Avatar != "")
                    user.UserInfo.Avatar = "/Content/UploadFiles/images/" + user.UserInfo.Avatar;
                else
                    user.UserInfo.Avatar = "/Content/UploadFiles/images/No_image_available.png";
                userInfoModel = user.UserInfo;
                using (var ncontext = new ApplicationDbContext())
                {
                    ncontext.Entry(userInfoModel).State = System.Data.Entity.EntityState.Modified;
                    ncontext.SaveChanges();
                };

                //update userRoles at aspNetRoles table
                ApplicationUser userFromDb = context.Users.Where(u => u.UserName.Equals(user.UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                var userRoles = UserManager.GetRoles(user.UserId);
                if (userRoles.Count() > 0)
                {
                    //remove user from current roles
                    foreach (var role in userRoles)
                    {
                        UserManager.RemoveFromRole(userFromDb.Id, role);
                    }
                    //add user to new roles
                    UserManager.AddToRole(user.UserId, user.Roles.SingleOrDefault().Name);
                }



                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Пример #2
0
        public HttpResponseMessage ChangePassword(ManagerUserViewModel user) //cap nhat user
        {
            try
            {
                ApplicationUser userFromDb = context.Users.Where(u => u.Id == user.UserId).FirstOrDefault();
                var responseMessage = new { title = "Lỗi đổi mật khẩu", message = "Error", isSuccess = false };
                if (UserManager.CheckPassword(userFromDb, user.Password))
                {
                    
                    IdentityResult result= UserManager.ChangePassword(user.UserId, user.Password, user.NewPassword);
                    context.SaveChanges();
                    if(result.Succeeded==true)
                        responseMessage = new { title = "Thành công", message = "Đổi mật khẩu thành công", isSuccess = true };
                    else
                     responseMessage = new { title = "Lỗi đổi mật khẩu", message = "Mật khẩu phải bao gồm cả số, chữ thường và chữ in hoa", isSuccess = false };
                   


                }
                else
                {responseMessage = new { title = "Lỗi đổi mật khẩu", message = "Mật khẩu cũ không đúng", isSuccess = false };
                   

                }
                return Request.CreateResponse(HttpStatusCode.OK, responseMessage);
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Пример #3
0
        public HttpResponseMessage GetAllUsers() //lay tat ca user
        {
            try
            {
                var userList = new List<ManagerUserViewModel>();
                foreach (var user in context.Users.ToList())
                {
                    var userRolesId = user.Roles.Select(m => m.RoleId).ToList();
                    var userId = user.Id;
                    var userModelInfo = context.Users.Where(m => m.Id == userId).Select(m => m.UserInfo).SingleOrDefault();
                    var model = new ManagerUserViewModel()
                    {
                        UserId = user.Id,
                        UserName = user.UserName,
                        Password = user.PasswordHash,
                        SecurityStamp = Guid.NewGuid().ToString(),
                        Email = user.Email,
                        UserInfo = userModelInfo,
                        Roles = context.Roles.Where(r => userRolesId.Contains(r.Id))
                                        .OrderBy(r => r.Name)
                                        .ToList()
                    };
                    userList.Add(model);
                }

                //var output = JsonConvert.SerializeObject(userList);
                return Request.CreateResponse(HttpStatusCode.OK, userList);
            }
            catch
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }