public async Task <ActionResult> Edit(int Id) { var user = await _userManager.FindByIdAsync(Id); if (user != null) { AccountViewModel model = new AccountViewModel(); model.UserName = user.UserName; model.FIO = user.FIO; model.Position = user.Position; model.SetPasswordEnabled = user.SetPasswordEnabled; model.LockoutEnabled = user.LockoutEnabled; //await userManager.UpdateAsync(user); IEnumerable <string> Roles = await _userManager.GetRolesAsync(Id); if (Roles != null) { model.UserRole = Roles.FirstOrDefault(); } else { model.UserRole = ""; } return(View(model)); } return(RedirectToAction("List")); }
public async Task <IHttpActionResult> GetUserInfo() { //var userClaim = Authentication.User; //var loginId = userClaim.Identity.GetUserId(); var loginId = User.Identity.GetUserId(); var user = await _userManager.FindByIdAsync(loginId); if (user == null) { return(NotFound()); } var listRole = await _userManager.GetRolesAsync(user.Id); UserInfoViewModel viewModel = new UserInfoViewModel { Id = user.Id, Email = user.Email, UserName = user.UserName, PhoneNumber = user.PhoneNumber, Roles = listRole, Address = user.Address }; return(Ok(viewModel)); }
public async Task <IdentityResult> RefreshUserGroupRolesAsync(long userId) { var user = await _userManager.FindByIdAsync(userId); if (user == null) { throw new ArgumentNullException("User"); } // Remove user from previous roles: var oldUserRoles = await _userManager.GetRolesAsync(userId); if (oldUserRoles.Count > 0) { //await _userManager.RemoveFromRolesAsync(userId, oldUserRoles.ToArray()); } // Find the roles this user is entitled to from group membership: var newGroupRoles = await this.GetUserGroupRolesAsync(userId); // Get the damn role names: var allRoles = await _roleManager.Roles.ToListAsync(); var addTheseRoles = allRoles.Where(r => newGroupRoles.Any(gr => gr.RoleId == r.Id)); var roleNames = addTheseRoles.Select(n => n.Name).ToArray(); // Add the user to the proper roles //await _userManager.AddToRolesAsync(userId, roleNames); return(IdentityResult.Success); }
private async Task <ClaimsPrincipal> getClaims(CustomerSite user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } var userId = await _myUserManager.GetUserIdAsync(user); var userName = await _myUserManager.GetUserNameAsync(user); var id = new ClaimsIdentity(); id.AddClaim(new Claim(JwtClaimTypes.Id, userId)); id.AddClaim(new Claim(JwtClaimTypes.PreferredUserName, userName)); var roles = await _myUserManager.GetRolesAsync(user); foreach (var roleName in roles) { id.AddClaim(new Claim(JwtClaimTypes.Role, roleName)); } id.AddClaims(await _myUserManager.GetClaimsAsync(user)); return(new ClaimsPrincipal(id)); }
public async Task <List <string> > GetRoleNamesByUserAsync(MyUser user) { if (user == null) { return(new List <string>()); } var roles = await uM.GetRolesAsync(user); return(roles.ToList()); }
public IHttpActionResult Get(int id) { AccountViewModel accountViewModel = new AccountViewModel(); /*_db.t_User.Where(w => w.UserID == id) * .Select(s => new AccountViewModel() {Id = s.UserID, UserName = s.UserName, FIO = s.FIO,Position = s.Position,SetPasswordEnabled = s.SetPasswordEnabled,LockoutEnabled = s.LockoutEnabled}) * .FirstOrDefault();*/ var user = _userManager.FindByIdAsync(id).Result; accountViewModel.Id = user.UserID; accountViewModel.UserName = user.UserName; accountViewModel.FIO = user.FIO; accountViewModel.Position = user.Position; accountViewModel.SetPasswordEnabled = user.SetPasswordEnabled; accountViewModel.LockoutEnabled = user.LockoutEnabled; string userRole = _userManager.GetRolesAsync(id).Result.FirstOrDefault(); if (userRole != null) { accountViewModel.UserRole = userRole; } return(Ok(accountViewModel)); }