The EU's top court has rejected a challenge by eastern European countries to a migrant relocation deal drawn up at the peak of the crisis in 2015.
The European Court of Justice overruled Hungary and Slovakia's objections to the compulsory fixed-quota scheme.
Hungary has not accepted a single asylum seeker since the measures were introduced two years ago.
They were an attempt to ease the pressure on frontline countries such as Greece and Italy.
Just under 28,000 people have been relocated under the scheme, rather than the 160,000 asylum seekers envisaged when it was agreed in September 2015.
Hungary, Slovakia, the Czech Republic and Romania voted against the quotas.
But EU officials insisted from the start that the scheme was binding on all member states regardless of whether they had voted for it or not. Hungary was asked to take 1,294 asylum seekers, Slovakia 802.
To date, Slovakia has accepted only about a dozen.
Why didn't Hungary and Slovakia want to take in the asylum seekers?
In asking the court to annul the deal, Hungary and Slovakia argued at the Court of Justice that there were procedural mistakes, and that quotas were not a suitable response to the migrant crisis.
Officials say the problem is not of their making, that the policy exposes them to a risk of Islamist terrorism and that it represents a threat to their homogenous societies.
Their case was supported by Poland, where a right-wing government has come to power since the 2015 deal.
But it was rejected by the ECJ ruling, which stated: "The Court dismisses the actions brought by Slovakia and Hungary against the provisional mechanism for the mandatory relocation of asylum seekers," it said.
"That mechanism actually contributes to enabling Greece and Italy to deal with the impact of the 2015 migration crisis and is proportionate."
The court's ruling is final and cannot be appealed.
Hungary, Poland and the Czech Republic are also facing legal action by the EU executive, the European Commission, for their inaction over the relocation of asylum seekers.
The three states could be referred to the European Court of Justice (ECJ) and eventually face heavy fines.
No quick-fix - Kevin Connolly, Europe correspondent, BBC News
The European Union regards the policy of relocating migrants who've crossed the Mediterranean as an important expression of political solidarity between member states.
Under the policy it set quotas for each country to accept fixed numbers of migrants to ease the direct burden on Italy and Greece - the usual points of arrival.
While Hungary and Slovakia have now failed in their legal challenge to the policy, that doesn't fix the political problem. The European Commission may still have to threaten financial penalties against countries that don't co-operate if the ruling from Luxembourg doesn't change their minds.
A note on terminology: The BBC uses the term migrant to refer to all people on the move who have yet to complete the legal process of claiming asylum. This group includes people fleeing war-torn countries such as Syria, who are likely to be granted refugee status, as well as people who are seeking jobs and better lives, who governments are likely to rule are economic migrants.