// GET: Home public async Task<ActionResult> Index() { var context = new ApplicationDbContext(); // DefaultConnection var store = new UserStore<CustomUser>(context); var manager = new UserManager<CustomUser>(store); var email = "[email protected]"; var password = "Passw0rd"; var user = await manager.FindByEmailAsync(email); if (user == null) { user = new CustomUser { UserName = email, Email = email, FirstName = "Super", LastName = "Admin" }; await manager.CreateAsync(user, password); } else { user.FirstName = "Super"; user.LastName = "Admin"; await manager.UpdateAsync(user); } return Content("Hello, Index"); }
public async Task<IdentityResult> UpdateUser(IdentityUser user) { IdentityResult user1 = null; try { _ctx = new AuthContext(); _userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(_ctx)); user1 = await _userManager.UpdateAsync(user); } catch (Exception ex) { } return user1; }
public async Task<WikiDownUser> Save(IPrincipal principal, UserManager<WikiDownUser> userManager) { var user = await userManager.FindByNameAsync(this.UserName); var roles = this.GetRoles(principal, user); if (user != null) { if (user.UserName == principal.Identity.Name) { var userAccessLevel = ArticleAccessHelper.GetAccessLevel(user.Roles); if (userAccessLevel < ArticleAccessLevel.Admin) { throw new HttpResponseException(HttpStatusCode.BadRequest); } } user.SetRoles(roles); user.SetDisplayName(this.DisplayName); user.SetEmail(this.Email); if (!string.IsNullOrWhiteSpace(this.Password)) { await userManager.RemovePasswordAsync(user.Id); await userManager.AddPasswordAsync(user.Id, this.Password); } await userManager.UpdateAsync(user); WikiDownUserCacheHelper.Clear(user.UserName); } else { user = new WikiDownUser(this.UserName) { Roles = roles }; user.SetDisplayName(this.DisplayName); user.SetEmail(this.Email); await userManager.CreateAsync(user, this.Password); } return user; }
public async Task<ActionResult> UpdateUser(string FirstName, string LastName, HttpPostedFileBase file) { var userId = User.Identity.GetUserId(); var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); var currentUser = manager.FindById(User.Identity.GetUserId()); if (file != null) { using (MemoryStream ms = new MemoryStream()) { file.InputStream.CopyTo(ms); byte[] array = ms.GetBuffer(); currentUser.Cover = array; currentUser.CoverType = file.ContentType; } } currentUser.LastName = LastName; currentUser.FirstName = FirstName; await manager.UpdateAsync(currentUser); return View("Index"); }
void DisableLockout(UserManager<IdentityUser> manager, IdentityUser identityUser) { if (identityUser != null) { identityUser.LockoutEnabled = true; identityUser.LockoutEndDateUtc = null; manager.UpdateAsync(identityUser); } }
void LockoutUntilYear3015(UserManager<IdentityUser> manager, IdentityUser identityUser) { if (identityUser != null) { identityUser.LockoutEnabled = true; identityUser.LockoutEndDateUtc = DateTime.UtcNow.AddYears(999); manager.UpdateAsync(identityUser); } }
public async Task<ActionResult> Create() { FitBit fitbit = new FitBit(); var userStore = new UserStore<ApplicationUser>(context); var userManager = new UserManager<ApplicationUser>(userStore); try { var currentUser = userManager.FindById(User.Identity.GetUserId()); var progress = Convert.ToDouble(currentUser.FitBitProgress); var goal = Convert.ToDouble(currentUser.FitBitGoal); var dateStart = Convert.ToDateTime(currentUser.dateStartFitBit.ToString()); var dateEnd = Convert.ToDateTime(currentUser.dateEndFitBit.ToString()); var percentEarned = (progress / goal) * 100; int percentEarnedShort = Convert.ToInt32(percentEarned); dateStart.ToShortDateString(); dateEnd.ToShortDateString(); fitbit.User = currentUser.UserName; fitbit.Progress = progress.ToString(); fitbit.Goal = goal.ToString(); fitbit.dateStart = dateStart; fitbit.dateEnd = dateEnd; fitbit.percentageEarned = percentEarnedShort.ToString(); db.FitBits.Add(fitbit); db.SaveChanges(); TempData["ValidationMessage"] = "Stats Submitted for " + " " + dateStart + " - " + dateEnd; var fitBitProgressNew = 0; currentUser.FitBitProgress = fitBitProgressNew; try { await userManager.UpdateAsync(currentUser); var saveUser = userStore.Context; await saveUser.SaveChangesAsync(); } catch { TempData["ValidationMessage"] = "Error: Stats Not Submitted!"; return RedirectToAction("Index", "Fitbit"); } return RedirectToAction("Manage", "Fitbit"); } catch { TempData["ValidationMessage"] = "Error: Stats Not Submitted!"; return RedirectToAction("Index", "Fitbit"); } }
public async Task<ActionResult> AddPhoneNumber(AddPhoneNumberViewModel model) { if (!ModelState.IsValid) { return View(model); } ApplicationDbContext db = new ApplicationDbContext(); var userStore = new UserStore<ApplicationUser>(db); var userManager = new UserManager<ApplicationUser>(userStore); var user = await userManager.FindByIdAsync(User.Identity.GetUserId()); if (user != null) { user.PhoneNumber = model.Number; } var result = await userManager.UpdateAsync(user); var ctx = userStore.Context; var result1 = await ctx.SaveChangesAsync(); // However, it always succeeds inspite of not updating the database if (!result.Succeeded) { AddErrors(result); } return RedirectToAction("Index", "Manage"); }
public async Task<ActionResult> updateDateFitBit(DateTime dateStart) { var userStore = new UserStore<ApplicationUser>(context); var userManager = new UserManager<ApplicationUser>(userStore); var currentUser = userManager.FindById(User.Identity.GetUserId()); var dateStartUpdate = dateStart.Date; //dateEnd is automatically 7 days after date start var dateEndUpdate = dateStartUpdate.Date.AddDays(7); currentUser.dateStartFitBit = dateStartUpdate.ToShortDateString(); currentUser.dateEndFitBit = dateEndUpdate.ToShortDateString(); try { await userManager.UpdateAsync(currentUser); var saveUser = userStore.Context; await saveUser.SaveChangesAsync(); TempData["ValidationMessage"] = "Date Start Set To: " + " " + dateStartUpdate.ToShortDateString(); } catch { TempData["ValidationMessage"] = "Error: Date Start Not Set!"; } return RedirectToAction("Index"); }
public async Task<ActionResult> updateGoalFitBit(string updateGoal) { var userStore = new UserStore<ApplicationUser>(context); var userManager = new UserManager<ApplicationUser>(userStore); var currentUser = userManager.FindById(User.Identity.GetUserId()); var fitBitUpdateGoal = Convert.ToInt32(updateGoal); currentUser.FitBitGoal = fitBitUpdateGoal; try { await userManager.UpdateAsync(currentUser); var saveUser = userStore.Context; await saveUser.SaveChangesAsync(); TempData["ValidationMessage"] = "Goal Updated To: " + " " + fitBitUpdateGoal; } catch { TempData["ValidationMessage"] = "Error: Goal Not Updated!"; } return RedirectToAction("Index"); }
public async Task<ActionResult> addProgressFitBit(string addProgress) { var userStore = new UserStore<ApplicationUser>(context); var userManager = new UserManager<ApplicationUser>(userStore); var currentUser = userManager.FindById(User.Identity.GetUserId()); var fitBitProgressOriginal = currentUser.FitBitProgress; var fitBitAdd = Convert.ToInt32(addProgress); var fitBitProgressNew = (fitBitProgressOriginal += fitBitAdd); currentUser.FitBitProgress = fitBitProgressNew; try { await userManager.UpdateAsync(currentUser); var saveUser = userStore.Context; await saveUser.SaveChangesAsync(); TempData["ValidationMessage"] = "Progress Added!"; } catch { TempData["ValidationMessage"] = "Error: Progress Not Added!"; } return RedirectToAction("Index"); }
public static async void ConfigureUsers(IEnumerable<InMemoryUser> users, EntityFrameworkServiceOptions options, UserManager userManager) { using(var db = new Contexto(options.ConnectionString)) { if(!db.Users.Any()) { foreach(var u in users) { var entity = new Usuario { Email = u.Claims.First(x=>x.Type==Constants.ClaimTypes.Email).Value , UserName = u.Username }; var response = await userManager.CreateAsync(entity, u.Password); if(!response.Succeeded) { throw new Exception("Não foi possível criar o usuario" + u.Username + response.Errors.ToString()); } else { var user = await userManager.FindAsync(u.Username,u.Password); foreach(var c in u.Claims) { await userManager.AddClaimAsync(user.Id,c); } await userManager.UpdateAsync(user); } } db.SaveChanges(); } } }
public async Task<ActionResult> Edit(EditViewModel model) { if (ModelState.IsValid) { var store = new UserStore<ApplicationUser>(new ApplicationDbContext()); var manager = new UserManager<ApplicationUser>(store); // Get the current application user var user = UserManager.FindById(User.Identity.GetUserId());; // Update the details user.FirstName = model.FirstName; user.LastName = model.LastName; user.EMail = model.EMail; user.City = model.City; user.Age = model.Age; // This is the part that doesn't work var result = await manager.UpdateAsync(user); var ctx = store.Context; ctx.SaveChanges(); // However, it always succeeds inspite of not updating the database if (!result.Succeeded) { AddErrors(result); } } return RedirectToAction("Manage"); }