public async Task <IActionResult> Register(RegisterData register) { //if desired role is admin, check to see if any there is a current admin. Allows first person to request admin status to get it if (register.Role == "admin") { if (await userManager.AdminCheck()) { return(BadRequest(new { message = "registration failed", errors = "Only Admin can assign role of Admin" })); } } var user = new ksUser { Email = register.Email, UserName = register.UserName, LoggedIn = true, }; var result = await userManager.CreateAsync(user, register.Password, register.Role); if (!result.Succeeded) { return(BadRequest(new { message = "registration failed", errors = result.Errors })); } return(Ok(await userManager.CreateUserWithToken(user))); }
public async Task <UserWithoutToken> CreateUserWithoutToken(ksUser user) { return(new UserWithoutToken { UserId = user.UserName, Channels = await GetUserChannels(user), Roles = (List <string>) await GetUserRoles(user), LastVisited = DateTime.Now, }); }
public async Task <List <createChannelDTO> > GetUserChannels(ksUser user) { var userChannels = await _context.UserChannels .Where(uc => uc.UserId == user.Id) .Select(uc => new createChannelDTO { name = uc.Channel.Name, type = uc.Channel.Type, }).ToListAsync(); return(userChannels); }
public async Task <IEnumerable <Message> > GetMyMessages(ksUser User) { var channels = await userChannelRepository.GetUserChannels(User); var channelNames = new List <string>(); channels.ForEach(channel => channelNames.Add(channel.name)); var messages = await _context.Messages .Where(msg => User.UserName == msg.Sender || User.UserName == msg.Recipient || channelNames.Contains(msg.Recipient)) .ToListAsync(); return(messages); }
public async Task <IdentityResult> CreateAsync(ksUser user, string password, string role) { if (role == "user" && !_context.Roles.Any(r => r.Name == "user")) { var newRole = new IdentityRole(); newRole.Name = "user"; await roleManager.CreateAsync(newRole); } var result = await userManager.CreateAsync(user, password); await userManager.AddToRoleAsync(user, role); await AddNewUserToGeneral(user.UserName); return(result); }
public async Task <IActionResult> UpdateUser(string userId, ksUser data) { var user = await userManager.FindByIdAsync(userId); if (user == null) { return(NotFound()); } await userManager.UpdateAsync(user); return(Ok(new { UserId = user.Id, user.Email, })); }
public async Task <string> CreateToken(ksUser user) { var secret = configuration["JWTSecret"]; var secretBytes = Encoding.UTF8.GetBytes(secret); var signingKey = new SymmetricSecurityKey(secretBytes); var roles = (List <string>) await userManager.GetRolesAsync(user); var tokenClaims = new[] { new Claim(JwtRegisteredClaimNames.Sub, user.UserName), new Claim("UserId", user.Id), new Claim(ClaimTypes.Role, roles[0]), }; var token = new JwtSecurityToken( expires: DateTime.UtcNow.AddSeconds(36000), claims: tokenClaims, signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256) ); var tokenString = new JwtSecurityTokenHandler().WriteToken(token); return(tokenString); }
public Task <IdentityResult> UpdateAsync(ksUser user) { return(userManager.UpdateAsync(user)); }
public Task <bool> CheckPasswordAsync(ksUser user, string password) { return(userManager.CheckPasswordAsync(user, password)); }
public Task AccessFailedAsync(ksUser user) { return(userManager.AccessFailedAsync(user)); }
public async Task <bool> IsUserAdmin(ksUser user) { return(await userManager.IsInRoleAsync(user, "admin")); }
public async Task <IList <string> > GetUserRoles(ksUser user) { var thisUser = await FindByNameAsync(user.UserName); return(await userManager.GetRolesAsync(thisUser)); }