public async Task <ActionResult> Edit([Bind(Include = "Id,Email,UserName,UserRole,FirstName,LastName")] AspNetUser aspNetUser) //,[Bind(Include=("Id,Name"))] As aspNetRole ) { if (ModelState.IsValid) { db.Entry(aspNetUser).State = EntityState.Modified; // await this.UserManager.AddToRoleAsync(aspNetUser.Id, aspNetUser.UserRole = aspNetUser.UserRole); var oldUser = this.UserManager.FindById(aspNetUser.Id); var oldRoleId = oldUser.Roles.SingleOrDefault().RoleId; // var oldRoleIdFk = oldUser.UserRoleFK.SingleOrDefault(); var oldRoleIdFk = oldUser.UserRoleFK.ToString(); var oldRoleName = db.AspNetRoles.SingleOrDefault(r => r.Id == oldRoleId).Name; if (oldRoleName != aspNetUser.UserRole) { this.UserManager.RemoveFromRole(aspNetUser.Id, oldRoleName); this.UserManager.AddToRole(aspNetUser.Id, aspNetUser.UserRole); var NewRoleId = (from Role in db.AspNetRoles where Role.Name == aspNetUser.UserRole select Role.Id).SingleOrDefault(); AspNetUser User = db.AspNetUsers.Single <AspNetUser>(u => u.Id == aspNetUser.Id); User.UserRoleFK = NewRoleId; } await db.SaveChangesAsync(); return(RedirectToAction("ManageAccounts")); } return(View(aspNetUser)); }
// Add the Refresh Token public async Task <bool> AddRefreshToken(RefreshToken token) { var existingToken = context.RefreshTokens.FirstOrDefault(r => r.UserName == token.UserName && r.ClientID == token.ClientID); if (existingToken != null) { var result = await RemoveRefreshToken(existingToken); } context.RefreshTokens.Add(token); return(await context.SaveChangesAsync() > 0 ? true: false); }
public async Task <ActionResult> Create([Bind(Include = "Id,StoreName,Location,ManagerIDFk,PhoneNumber,DateOpened")] Store store) { if (ModelState.IsValid) { db.Stores.Add(store); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.ManagerIDFk = new SelectList(db.AspNetUsers, "Id", "Email", store.ManagerIDFk); return(View(store)); }