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); } }
//[ValidateAntiForgeryToken] public async Task<ActionResult> Register(RegisterViewModel model)//RegisterViewModel model { var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())); if (ModelState.IsValid) { model.Available = true; if (model.Avatar != null && model.Avatar.ToString() != "") model.Avatar = "/Content/UploadFiles/images/" + model.Avatar; else model.Avatar = "/Content/images/No_image_available.png"; var userInfo = new UserInfo() { Email = model.Email, Avatar = model.Avatar, Address = model.Address, Skype = model.Skype, Yahoo = model.Yahoo, Facebook = model.Facebook, Available = model.Available }; var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; user.UserInfo = userInfo; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //if (!roleManager.RoleExists("Administrator")) //{ // var role = new IdentityRole("Administrator"); // var roleResult = await roleManager.CreateAsync(role); // //await UserManager.CreateAsync(user, "123456"); // var userId = UserManager.FindByName(model.UserName).Id; // UserManager.AddToRole(userId, "Administrator"); //} //else //{ //var role = new IdentityRole("Users"); //var roleResult = await roleManager.CreateAsync(role); var userId = UserManager.FindByName(model.UserName).Id; UserManager.AddToRole(userId, model.RoleName); //} //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return new HttpStatusCodeResult(HttpStatusCode.OK); //return RedirectToAction("Index", "Home"); } return new HttpStatusCodeResult(HttpStatusCode.BadRequest); //AddErrors(result); } // If we got this far, something failed, redisplay form return new HttpStatusCodeResult(HttpStatusCode.BadRequest); //return View(model); }