public UserAuthorizationM Login(UserLoginM model) { try { if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Password)) { throw BadRequest("Username and Password must not empty!"); } if (model.Username.Length < 3 || model.Password.Length < 3) { throw BadRequest("Username and Password must have more than 3 characters!"); } User user = _user.Where(u => u.Username.Equals(model.Username)) .Select(u => new User { Id = u.Id, Username = u.Username, Password = u.Password, AdminUserId = u.AdminUserId }).FirstOrDefault(); if (user == null) { throw BadRequest("Username or password is incorrect!"); } bool result = ProjectManagementAuthentication.VerifyHashedPassword(user.Username, user.Password, model.Password, out string rehashed_password); if (!result) { throw BadRequest("Username or password is incorrect!"); } if (rehashed_password != null) { user.Password = rehashed_password; } SaveChanges(); return(new UserAuthorizationM { User = new UserM { Id = user.Id, Username = user.Username }, AdminUser = user.AdminUserId == null ? null : _user.Where(u => u.Id.Equals(user.AdminUserId.Value)).Select(u => new UserM { Id = u.Id, Username = u.Username }).FirstOrDefault() }); } catch (Exception e) { throw e is RequestException ? e : _errorHandler.WriteLog("An error occurred while log in!", e, DateTime.Now, "Server", "Service_User_Login"); } }
private void TestLoginException(UserLoginM model, string expected) { try { _user.Login(model); } catch (System.Exception e) { Assert.AreEqual(expected, ((RequestException)e).Error.Detail.InnerMessage); } }
public IActionResult Login([FromQuery] string redirect_uri, [FromBody] UserLoginM model) { try { string role = ApplicationRole.Web_User; UserAuthorizationM result = _user.Login(model); if (model.Username.Equals(ApplicationAuth.Nococid_Application_Admin)) { role = ApplicationRole.Application_Admin; } result.Jwt = _jwtAuth.GenerateJwt(result.AdminUser == null ? Guid.Empty : result.AdminUser.Id, result.User.Id, role); if (string.IsNullOrEmpty(redirect_uri)) { return(Ok(result)); } return(Redirect(redirect_uri + "?user=nococid&jwt=" + result.Jwt)); } catch (Exception e) { return(GetError(e)); } }