public IActionResult RenewToken() { try { // check if claimtype Exist in the Token bool hasUsernamer = HttpContext.User.HasClaim(c => c.Type == ClaimTypes.NameIdentifier); if (!hasUsernamer) { throw new SecurityTokenException("Issue with Token (no ClaimTypes.NameIdentifier (" + where + ") (RENEW)"); } // get back value ID from the Token & tryto Parse into Int string valueUsername = HttpContext.User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int id2w; bool ParseSuccess = int.TryParse(valueUsername, out id2w); if (!ParseSuccess) { throw new SecurityTokenException("Issue with Token (TryParse)" + valueUsername + " (" + where + ") (RENEW)"); } // Get back user data SM.User u = S.ServiceLocator.Instance.usersService.Get(id2w); if (u is null) { throw new AuthenticationException("Record not found for Renew Token (" + id2w.ToString() + ") (" + where + ") (RENEW)"); } return(ApiControllerHelper.SendOk(this, new ApiResult <JWT_Bearer>(HttpStatusCode.OK, null, GenToken(u)), true)); } catch (Exception ex) { return(ApiControllerHelper.SendError(this, ex)); } }
/// <summary> /// Generate Token according to User information /// </summary> /// <param name="u">User Object, to user ID,EMail,Status </param> /// <returns>New Token</returns> private JWT_Bearer GenToken(SM.User u) { JWT_Bearer JWTB = new JWT_Bearer(); if (u != null) { JwtService jwt = new JwtService(_config); JWTB.id = u.Id; JWTB.ExpirationDateTime = DateTime.Now.AddMinutes(double.Parse(_config.GetSection("JwtConfig").GetSection("expirationInMinutes").Value)); JWTB.BearerJWT = jwt.GenerateSecurityToken(u.Email, u.Status.ToString(), u.Id. ToString());; } return(JWTB); }
public IActionResult Upd(int id, [FromBody] user_upd uupd) { try { if (id < 1) { throw new IndexOutOfRangeException("ID must be greater than 0 (" + where + ") (UPD)"); } if (uupd.FirstName == uupd.LastName) { ModelState.AddModelError("LastName", "The last name cannot be the same as the first name."); } //if (uupd.Status > 1) throw new DataException("Wrong USer Status"); // get user to get all dtata enad mod only need dtat to update and pass user object SM.User u = S.ServiceLocator.Instance.usersService.Get(id); if (u is null) { throw new AuthenticationException("Record not found for update (" + id.ToString() + ") (" + where + ") (RESET)"); } // if email orign VS emailupd <> then check email unique //if (u.Email != uupd.Email) //{ // bool EmailOK = S.ServiceLocator.Instance.usersService.EmailIsUsed(uupd.Email); // if (EmailOK) throw new ValidationException("Email already used :" + uupd.Email); //} u.Id = id; u.FirstName = uupd.FirstName; u.LastName = uupd.LastName; u.Status = uupd.Status; u.Avatar = uupd.Avatar; bool UpdOk = S.ServiceLocator.Instance.usersService.Upd(id, u);; return(ApiControllerHelper.SendOk(this, new ApiResult <bool>(HttpStatusCode.OK, null, UpdOk), HttpStatusCode.OK)); } catch (Exception ex) { return(ApiControllerHelper.SendError(this, ex)); } }
public IActionResult Get(int id) { try { if (id < 1) { throw new IndexOutOfRangeException("ID must be greater than 0 (" + where + ") (GET)"); } SM.User u = S.ServiceLocator.Instance.usersService.Get(id); return(ApiControllerHelper.SendOk(this, new ApiResult <SM.User>(HttpStatusCode.OK, null, u), true)); } catch (Exception ex) { return(ApiControllerHelper.SendError(this, ex)); } }
public IActionResult Login([FromBody] Login l) { try { SM.User u = S.ServiceLocator.Instance.usersService.Login(l.Email, Base64.Base64Decode(l.Passwd)); if (u is null) { throw new AuthenticationException("Wrong Login/passwd (" + where + ") (LOGIN)"); } //return Ok(new ApiResult<JWT_Bearer>(HttpStatusCode.OK, null, GenToken(u))); return(ApiControllerHelper.SendOk(this, new ApiResult <JWT_Bearer>(HttpStatusCode.OK, null, GenToken(u)), true)); } catch (Exception ex) { return(ApiControllerHelper.SendError(this, ex)); } }
public IActionResult Add([FromBody] user_add uadd) { try { if (uadd.FirstName == uadd.LastName) { throw new ValidationException("The last name cannot be the same as the first name. (" + where + ") (RESET)"); } if (uadd.Email.Length == 0) { throw new ValidationException("Email empty"); } // checkif email is not taken bool EmailOK = S.ServiceLocator.Instance.usersService.EmailIsUsed(uadd.Email); if (EmailOK) { throw new ValidationException("Email already used :" + uadd.Email); } SM.User u = new SM.User(); u.FirstName = uadd.FirstName; u.LastName = uadd.LastName; u.Email = uadd.Email; u.Passwd = Base64.Base64Decode(uadd.Passwd); u.SecretAnswer = Base64.Base64Decode(uadd.SecretAnswer); u.Avatar = uadd.Avatar; u = S.ServiceLocator.Instance.usersService.Add(u); u.Passwd = ""; /* put passwd BLANK */ u.SecretAnswer = ""; /* put Secret BLANK */ return(ApiControllerHelper.SendOk(this, new ApiResult <SM.User>(HttpStatusCode.OK, null, u), true)); } catch (Exception ex) { return(ApiControllerHelper.SendError(this, ex)); } }