public IActionResult Login(AuthenticationViewModel model) { // Assume the user is not authorized IActionResult result = Unauthorized(); try { var roleMgr = new RoleManager(_db) { //User = _db.GetUserItemByEmail(model.Email) User = _db.GetUserItemByLogin(model.EmailOrUsername) }; // Generate a password hash var pwdMgr = new PasswordManager(model.Password, roleMgr.User.Salt); if (pwdMgr.Verify(roleMgr.User.Hash)) { // Create an authentication token var token = _tokenGenerator.GenerateToken(roleMgr.User.Username, roleMgr.RoleName); // Switch to 200 OK result = Ok(token); } } catch (Exception) { result = BadRequest(new { Message = "Username or password is invalid." }); } return(result); }
//[Authorize] public IActionResult CheckIn(string userName) { IActionResult result = Unauthorized(); try { UserItem myUser = _db.GetUserItemByLogin(userName); // UserItem myUser = _db.GetUserItemByLogin(User.Identity.Name); VisitItem newVisit = new VisitItem() { UserId = myUser.Id, CheckIn = DateTime.UtcNow }; newVisit.Id = _db.CreateVisit(newVisit); // Switch to 200 OK if (newVisit.Id > 0) { result = Ok(newVisit.Id); } } catch (Exception) { result = BadRequest(new { Message = "Failed to checkin user" }); } return(result); }
//[Authorize(Roles = "Admin")] public IActionResult PromoteUser(string userName, int roleId) { IActionResult result = Unauthorized(); try { if (roleId < 4 && roleId > 0) { _db.UpdateUserRole(_db.GetUserItemByLogin(userName), roleId); } result = Ok(); } catch (Exception) { result = BadRequest(new { Message = "Role update failed." }); } return(result); }
public IActionResult DisplayUserHistory(string userName) { // Assume the user is not authorized IActionResult result = Unauthorized(); try { var user = _db.GetUserItemByLogin(userName); var history = _db.GetUserHistory(user); result = Ok(history); } catch (Exception) { result = BadRequest(new { Message = "Get history for user failed." }); } return(result); }