/// <summary> /// Find user by provided username /// </summary> /// <param name="userName"></param> /// <returns></returns> public async Task <GradeBookUser> FindUserByUserName(string userName) { logger.Trace("Find User {@userName}", userName); GradeBookUser user = await _userManager.FindByNameAsync(userName); return(user); }
/// <summary> /// Find user for provided username AND password /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <returns></returns> public async Task <GradeBookUser> FindUser(string userName, string password) { logger.Trace("Find User {@userName}", userName); GradeBookUser user = await _userManager.FindAsync(userName, password); return(user); }
/// <summary> /// Common user update method, /// will not work for special properties! /// It IS working, after all. /// Only you need special Dto for controller and service, to update the special properties! /// </summary> /// <param name="user"></param> /// <returns></returns> public async Task <IdentityResult> UpdateUser(GradeBookUser user) { // NOTE this will not work for concrete users // It will only update the common properties, not the special ones.... // UPDATE It is working, indeed var result = await _userManager.UpdateAsync(user); return(result); }
/// <summary> /// Used by the WhoAmI and WhoIs endpoints /// </summary> /// <param name="userId"></param> /// <returns></returns> public UserDataDto GetUserData(int userId) { GradeBookUser user = db.GradeBookUsersRepository.GetByID(userId); if (user == null) { return(null); } UserDataDto dataDto = new UserDataDto() { UserId = user.Id, UserName = user.UserName, FirstName = user.FirstName, LastName = user.LastName }; if (user is AdminUser) { dataDto.Role = UserRole.ADMIN; } else if (user is TeacherUser) { dataDto.Role = UserRole.TEACHER; } else if (user is StudentUser) { dataDto.Role = UserRole.STUDENT; } else if (user is ParentUser) { dataDto.Role = UserRole.PARENT; } return(dataDto); }
/// <summary> /// Fill the claims inside the token /// </summary> /// <param name="context"></param> /// <param name="user"></param> /// <param name="roles"></param> /// <returns></returns> private static ClaimsIdentity SetClaimsIdentity(OAuthGrantResourceOwnerCredentialsContext context, GradeBookUser user, IEnumerable <string> roles) { // Just for reference: context.Options.AuthenticationType var identity = new ClaimsIdentity("JWT"); identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("UserId", user.Id.ToString())); foreach (var role in roles) { identity.AddClaim(new Claim(ClaimTypes.Role, role)); } return(identity); }