Colm Whelan scored a sublime winner to celebrate his inclusion in the Republic of Ireland under-21 squad announced earlier in the day.
The Kilkenny native's 17th goal of the season keeps promotion chasing UCD’s season very much on the boil.
It also avenged last season’s promotion play-off defeat to Longford as Andy Myler’s side deservedly advanced to the quarter-finals of the Extra.ie FAI cup.
Defeat all but ends Longford’s season as they languish 16 points adrift at the foot of the Premier Division table.
With four changes from last week’s heavy league defeat at Waterford, Longford started positively enough, Dylan Grimes forcing an 11th minute save from Lorcan Healy.
A slick move four minutes later was inches from giving the midlanders the lead. Darragh Nugent, Dean Williams and Grimes combined on the right with Conor Davis' shooting just past Healy’s right-hand post.
In-form UCD didn’t come alive until the 23rd minute; Whelan and Liam Kerrigan in turn having drives bravely blocked by Town defender Joe Manley.
Minutes later Adam Verdon’s snatched shot, after Longford failed to clear Paul Doyle’s cross, arrowed off target.
The inventive Grimes wasn’t too far wide with a speculative free kick on the half hour as Longford were having to work hard to reassert their early grip on the game.
But it was UCD who finished the half on the front foot. Doyle touched a free kick to left-back Evan Weir whose thumping drive from distance was batted away by Town keeper Mick Kelly. Kerrigan’s follow-up shot flashed past a post.
And the home began the second half where they left off; skipper Jack Keaney rattling Kelly’s crossbar with a 30-yard free-kick 10 minutes in after Aaron Robinson was carded for a foul on Kerrigan.
Another card for a foul on Kerrigan, this time by Joe Manley, delivered UCD’s lead on 70 minutes.
Having anchored their midfield superbly, Doyle stepped up to completely deceive Kelly with a well-driven low left-footed free kick that arced into the far corner of the net.
College's advantage lasted just three minutes, though, as Longford hit back.
Rob Manley did well on the left to whip over a low cross for Williams to finish from close range.
Williams brought a good save from Healy with a shot on the turn before Whelan’s moment arrived on 80 minutes.
Collecting the ball some 25 yards out, the First Division's leading scorer hit a stunning strike that gave Kelly no chance as it powered to the roof of the net.
UCD: Lorcan Healy; Evan Osam, Harvey O'Brien, Sam Todd, Evan Weir; Paul Doyle, Jack Keaney; Liam Kerrigan, Adam Verdon (Dara Keane 69), Mark Dignam; Colm Whelan.
Longford Town: Mick Kelly; Joe Manley, Aaron O’Driscoll, Mick McDonnell, Paddy Kirk (Callum Warfield 83); Dylan Grimes, Darragh Nugent (Dean Byrne 87), Aaron Robinson (Dean Zambra 74); Dean Williams, Conor Davis (Aaron Dobbs 74), Rob Manley.
Referee: Ariano Reale (Kildare).