public async Task <ApiResult <UserViewModelApi> > SignIn([FromBody] SignInViewModel viewModel) { if (ModelState.IsValid) { AppUser user = await _userManager.FindUserWithRolesByNameAsync(viewModel.UserName); if (user == null) { return(BadRequest(NotificationMessages.UserNotFound)); } bool result = await _userManager.CheckPasswordAsync(user, viewModel.Password); if (!result) { return(BadRequest(NotificationMessages.InvalidUserNameOrPassword)); } UserViewModelApi userViewModel = await _userManager.FindUserApiByIdAsync(user.Id); userViewModel.Image = $"{Request.Scheme}://{Request.Host}{Request.PathBase.Value}/wwwroot/Users/{userViewModel.Image}"; userViewModel.Token = await _jwtService.GenerateAccessTokenAsync(user); return(Ok(userViewModel)); } return(BadRequest(ModelState.GetErrorsModelState())); }
public async Task <LogicResult> UpdateUserProfile(UserViewModelApi viewModel) { LogicResult logicResult = new LogicResult(); AppUser user = await FindByIdAsync(viewModel.Id.ToString()); if (user != null) { logicResult.MessageType = MessageType.Success; logicResult.Message.Add(NotificationMessages.OperationSuccess); user.FirstName = viewModel.FirstName; user.LastName = viewModel.LastName; user.UserName = viewModel.UserName; user.PhoneNumber = viewModel.PhoneNumber; user.BirthDate = !string.IsNullOrWhiteSpace(viewModel.PersianBirthDate) ? viewModel.PersianBirthDate.ConvertPersianToGeorgian() : user.BirthDate; user.Gender = viewModel.Gender; user.Email = viewModel.Email; await UpdateAsync(user); } else { logicResult.MessageType = MessageType.Error; logicResult.Message.Add(NotificationMessages.UserNotFound); } return(logicResult); }
//[JwtAuthentication(Policy = ConstantPolicies.DynamicPermission)] public async Task <ApiResult <UserViewModelApi> > GetUserLoggedIn() { if (User.Identity.IsAuthenticated) { UserViewModelApi user = await _userManager.FindUserApiByIdAsync(User.Identity.GetUserId <int>()); return(Ok(user)); } return(BadRequest(NotificationMessages.UserNotFound)); }
public async Task <ApiResult <string> > EditUser([FromBody] UserViewModelApi viewModel) { try { if (ModelState.IsValid) { IdentityResult result; AppUser user = new AppUser(); viewModel.BirthDate = viewModel.PersianBirthDate.ConvertPersianToGeorgian(); user = await _userManager.FindByIdAsync(viewModel.Id.ToString()); var userRoles = await _userManager.GetRolesAsync(user); result = await _userManager.RemoveFromRolesAsync(user, userRoles); if (result.Succeeded) { user.UserName = viewModel.UserName; user.FirstName = viewModel.FirstName; user.LastName = viewModel.LastName; user.BirthDate = viewModel.BirthDate; user.Email = viewModel.Email; user.PhoneNumber = viewModel.PhoneNumber; user.IsActive = viewModel.IsActive; if (viewModel.Gender != null) { user.Gender = viewModel.Gender.Value; } result = await _userManager.UpdateAsync(user); await _userManager.UpdateSecurityStampAsync(user); } } else { throw new AppException(ModelState.GetErrorsModelState()); } } catch (Exception e) { throw new AppException(e.Message); } return(Ok(NotificationMessages.CreateSuccess)); }
public async Task <ApiResult <string> > UpdateUserProfile([FromBody] UserViewModelApi viewModel) { LogicResult logicResult = new LogicResult(); try { logicResult = await _userManager.UpdateUserProfile(viewModel); if (logicResult.MessageType == MessageType.Success) { return(Ok(logicResult.Message.FirstOrDefault())); } } catch (Exception ex) { logicResult.MessageType = MessageType.Error; logicResult.Message.Add(ex.Message); } return(BadRequest(logicResult.Message.FirstOrDefault())); }
public async Task <ApiResult <string> > DeleteUser([FromBody] UserViewModelApi viewModel) { try { var user = await _userManager.FindByIdAsync(viewModel.Id.ToString()); if (user == null) { throw new AppException(NotificationMessages.UserNotFound); } else { await _userManager.DeleteAsync(user); } } catch (Exception e) { throw new AppException(e.Message); } return(Ok(NotificationMessages.OperationSuccess)); }
public async Task <ResponseTokenViewModel> AuthenticateUser(HttpRequest request, RequestTokenViewModel requestToken) { string ipAddress = _httpContextAccessor.HttpContext.Connection?.RemoteIpAddress.ToString(); ResponseTokenViewModel responseTokenViewModel = new ResponseTokenViewModel(); if (requestToken.GrantType == "Password") { AppUser user = await _userManager.FindUserWithRolesByNameAsync(requestToken.UserName); if (user == null) { responseTokenViewModel.IsSuccess = false; responseTokenViewModel.Message = NotificationMessages.UserNotFound; responseTokenViewModel.ApiStatusCode = ApiResultStatusCode.UnAuthorized; //throw new AppException(ApiResultStatusCode.UnAuthorized, NotificationMessages.UserNotFound, HttpStatusCode.BadRequest); } else { bool result = await _userManager.CheckPasswordAsync(user, requestToken.Password); if (!result) { responseTokenViewModel.IsSuccess = false; responseTokenViewModel.Message = NotificationMessages.InvalidUserNameOrPassword; responseTokenViewModel.ApiStatusCode = ApiResultStatusCode.UnAuthorized; // throw new AppException(ApiResultStatusCode.UnAuthorized, NotificationMessages.InvalidUserNameOrPassword, HttpStatusCode.BadRequest); } else { UserViewModelApi userViewModel = await _userManager.FindUserApiByIdAsync(user.Id); userViewModel.Image = $"{request.Scheme}://{request.Host}{request.PathBase.Value}/wwwroot/Users/{userViewModel.Image}"; RefreshToken oldRefreshToken = await _refreshTokenService.GetRefreshTokenByUserIdAsync(user.Id); if (oldRefreshToken != null) { await _refreshTokenService.RemoveRefreshTokenAsync(oldRefreshToken); } RefreshToken refreshToken = _refreshTokenService.CreateRefreshToken(_siteSettings.RefreshTokenSetting, user.Id, requestToken.IsRemember, ipAddress); await _refreshTokenService.AddRefreshTokenAsync(refreshToken); responseTokenViewModel.AccessToken = await GenerateAccessTokenAsync(user); responseTokenViewModel.RefreshToken = refreshToken.Value; responseTokenViewModel.User = userViewModel; responseTokenViewModel.IsSuccess = true; } } } else if (requestToken.GrantType == "RefreshToken") { responseTokenViewModel = await GenerateAccessAndRefreshToken(requestToken, ipAddress); } else { responseTokenViewModel.IsSuccess = false; responseTokenViewModel.ApiStatusCode = ApiResultStatusCode.NotFound; responseTokenViewModel.Message = NotificationMessages.UserNotFound; //throw new AppException(ApiResultStatusCode.BadRequest, NotificationMessages.TargetNotFounded, HttpStatusCode.BadRequest); } return(responseTokenViewModel); }
public async Task <ApiResult <string> > CreateUser([FromBody] UserViewModelApi viewModel) { try { if (ModelState.IsValid) { IdentityResult result; AppUser user = new AppUser(); if (!string.IsNullOrWhiteSpace(viewModel.PersianBirthDate)) { viewModel.BirthDate = viewModel.PersianBirthDate.ConvertPersianToGeorgian(); } user.EmailConfirmed = true; user.UserName = viewModel.UserName; user.FirstName = viewModel.FirstName; user.LastName = viewModel.LastName; user.PasswordHash = viewModel.Password; user.Email = viewModel.Email; user.BirthDate = viewModel.BirthDate; user.PhoneNumber = viewModel.PhoneNumber; user.IsActive = true; //user.Image = viewModel.File != null ? viewModel.File.FileName : ""; if (viewModel.Gender != null) { user.Gender = viewModel.Gender.Value; } result = await _userManager.CreateAsync(user, viewModel.Password); if (result.Succeeded) { var role = await _roleManager.FindByIdAsync(viewModel.RoleId.ToString()); if (role != null) { await _userManager.AddToRoleAsync(user, role.Name); } } //if (result.Succeeded) //{ // logicResult.MessageType = MessageType.Success; // logicResult.Message.Add(NotificationMessages.CreateSuccess); //} //else //{ // logicResult.MessageType = MessageType.Error; // logicResult.Message.Add(result.DumpErrors()); //} } else { throw new AppException(ModelState.GetErrorsModelState()); } } catch (Exception e) { throw new AppException(e.Message); } return(Ok(NotificationMessages.CreateSuccess)); }