public ActionResult <OneDataTransfer <User> > Get(string id) { OneDataTransfer <User> ret = new OneDataTransfer <User>(); User user = new User(); UserManager userManager = new UserManager(); int CodError = 0; string ErrorMessage = string.Empty; user = userManager.GetUserById(id, ref CodError, ref ErrorMessage); if (CodError != 0) { ret.code = CodError; ret.message = ErrorMessage; return(NotFound(ret)); } if (user == null && user.IdUser == 0) { ret.data = null; ret.code = -98; ret.message = "Id Not found"; return(NotFound(ret)); } ret.data = user; ret.code = 0; ret.message = "OK"; return(Ok(ret)); }
public ActionResult <OneDataTransfer <User> > Put(string id, [FromBody] UserUpdate user) { OneDataTransfer <User> response = new OneDataTransfer <User>(); try { UserManager userManager = new UserManager(); string errorMessage = string.Empty; int errorCode = 0; userManager.UpdateUser(id, user, ref errorCode, ref errorMessage); if (errorCode != 0) { response.code = errorCode; response.message = errorMessage; return(NotFound(response)); } response.code = errorCode; response.message = "OK"; return(Ok(response)); } catch (Exception ex) { response.code = -100; response.message = ex.Message; return(BadRequest(response)); } }
public ActionResult <OneDataTransfer <User> > Post([FromBody] UserInsert user) { OneDataTransfer <User> response = new OneDataTransfer <User>(); try { int errorCode = 0; string errorMessage = "OK"; UserManager userManager = new UserManager(); //encrypt password SecurityRSA rSA = new SecurityRSA(); string pubKey = rSA.GeneratePublicKey(); string encryptedPassword = rSA.Encrypt(pubKey, user.password); user.password = encryptedPassword; string IdUser = userManager.InsertUser(user, ref errorCode, ref errorMessage); if (errorCode != 0) { response.code = errorCode; response.message = errorMessage; return(BadRequest(response)); } response.code = errorCode; response.message = "OK"; return(CreatedAtRoute("getuser", new { id = IdUser }, response)); } catch (Exception ex) { response.code = -100; response.message = ex.Message; return(BadRequest(response)); } }
public ActionResult <OneDataTransfer <object> > userLogin([FromBody] UserLogin userLogin) { OneDataTransfer <object> response = new OneDataTransfer <object>(); try { int errorCode = 0; string errorMessage = "OK"; UserManager userManager = new UserManager(); User user = userManager.GetUserByLogin(userLogin.UserName, ref errorCode, ref errorMessage); SecurityRSA rSA = new SecurityRSA(); string pubKey = rSA.GeneratePublicKey(); string decryptedPass = rSA.Decrypt(user.Password); if (decryptedPass == userLogin.Password) { //Get JWT var claim = new[] { new Claim(JwtRegisteredClaimNames.Sub, user.NickName) }; var signinKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Aquivaunallaveconlaquequieroencriptar")); int expiryInMinutes = 5; //Minutes to expired var token = new JwtSecurityToken( issuer: "http://www.ordersjsp.com.co", audience: "http://www.ordersjsp.com.co", expires: DateTime.UtcNow.AddMinutes(expiryInMinutes), signingCredentials: new SigningCredentials(signinKey, SecurityAlgorithms.HmacSha256) ); response.data = new { token = new JwtSecurityTokenHandler().WriteToken(token), expiration = token.ValidTo }; response.code = errorCode; response.message = "OK"; return(Ok(response)); } else { response.code = errorCode; response.message = errorMessage; return(Unauthorized()); } } catch (Exception ex) { response.code = -100; response.message = ex.Message; return(BadRequest(response)); } }
public ActionResult <OneDataTransfer <User> > Delete(string id) { string errorMessage = string.Empty; int errorCode = 0; OneDataTransfer <User> response = new OneDataTransfer <User>(); try { UserManager userManager = new UserManager(); User userExists = userManager.GetUserById(id, ref errorCode, ref errorMessage); if (errorCode != 0) { response.data = null; response.code = errorCode; response.message = "TODO: Error"; return(NotFound()); } if (userExists == null) { response.data = null; response.code = errorCode; response.message = "User Not Found"; return(NotFound(response)); } userManager.DeleteUser(id, ref errorCode, ref errorMessage); if (errorCode != 0) { response.code = errorCode; response.message = errorMessage; return(NotFound(response)); } response.code = errorCode; response.message = "OK"; return(NoContent()); } catch (Exception ex) { response.code = -100; response.message = ex.Message; return(BadRequest(response)); } }