public async Task <IActionResult> GetMembershipByMemberUserName(Guid groupId, string memberUserName) { User loggedOnUser = await _identityBusiness.GetUserAsync(User); var loggedOnUserIsGroupOwner = await _taskBookBusiness.IsUserGroupOwner(loggedOnUser.Id, groupId); if (!loggedOnUserIsGroupOwner) { return(NotFound()); } User memberUser = await _identityBusiness.FindByNameAsync(memberUserName); if (memberUser == null) { return(NotFound()); } UserGroup membership = await _taskBookBusiness.GetGroupMembership(memberUser.Id, groupId); if (membership == null) { return(NotFound()); } var viewModel = _mapper.Map <GroupMembershipViewModel>(membership); return(Ok(viewModel)); }
public async Task <IActionResult> GetProfile(string userName) { User user = await _identityBusiness.FindByNameAsync(userName); if (user == null) { return(NotFound()); } User loggedOnUser = await _identityBusiness.GetUserAsync(User); if (user.Id != loggedOnUser.Id) { return(Forbid()); } var profile = _mapper.Map <ProfileViewModel>(loggedOnUser); return(Ok(profile)); }
public async Task <IActionResult> Logon([FromBody] LogonParameters parameters) { if (parameters == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(UnprocessableEntity(ModelState)); } SignInResult result = await _identityBusiness.PasswordSignInAsync(parameters.UserName, parameters.Password); if (!result.Succeeded) { return(Unauthorized()); } var user = await _identityBusiness.FindByNameAsync(parameters.UserName); var claims = new[] { new Claim(JwtRegisteredClaimNames.Sub, user.UserName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()) }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.SecurityKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: _jwtOptions.Issuer, audience: _jwtOptions.Audience, claims: claims, expires: DateTime.Now.AddMinutes(_jwtOptions.ExpiresMinutes), signingCredentials: creds); return(Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token) })); }
public async Task <IActionResult> ChangePassword( [FromRoute] string userName, [FromBody] ChangePasswordParameters parameters) { if (parameters == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(UnprocessableEntity(ModelState)); } User user = await _identityBusiness.FindByNameAsync(userName); if (user == null) { return(NotFound()); } User loggedOnUser = await _identityBusiness.GetUserAsync(User); if (user.Id != loggedOnUser.Id) { return(Forbid()); } IdentityResult result = await _identityBusiness.ChangePasswordAsync(loggedOnUser, parameters.CurrentPassword, parameters.NewPassword); if (result.Succeeded) { return(NoContent()); } foreach (IdentityError error in result.Errors) { ModelState.AddModelError(error.Code, error.Description); } return(UnprocessableEntity(ModelState)); }