Gov. Andrew Cuomo on Monday night signed legislation granting driver’s licenses to illegal immigrants — shortly after the controversial measure passed the state Senate.

Cuomo’s action came despite throwing supporters a last minute curveball by asking the state’s top civil attorney, Solicitor General Barbara Underwood, to review the measure for possible safety concerns — threatening to veto it if he didn’t like her assessment.

“You could create a database for the feds to use to actually track down undocumented people,” Cuomo said on WAMC radio. “California passed a law, and they are now in litigation.”

But Underwood’s boss, Attorney General Tish James, later released a statement amid the Monday-night vote arguing that the bill is legally sound.

“The legislation is well-crafted and contains ample protections for those who apply for driver’s licenses. If this bill is enacted and challenged in court, we will vigorously defend it,” she said.

The law takes 180 days to go into effect, meaning the first licenses will be available in December.

The measure on Monday passed the state Senate by a 33-29 count, often eliciting emotional remarks from both sides of the aisle during the floor vote.