A federal appeals court ruled Wednesday that the Justice Department can refuse to give crime-fighting money to cities and states that consider themselves sanctuaries and refuse to share information with federal immigration authorities.

The unanimous ruling from a three-judge panel of the 2nd U.S. Circuit Court of Appeals was a defeat for New York City and seven states — Connecticut, Rhode Island, Massachusetts, New Jersey, New York, Virginia and Washington. They sued after the Justice Department said in 2017 that it would not award grants from a federal program to local governments that withheld information about undocumented immigrants in their jails.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

"It recognizes the lawful authority of the attorney general to ensure that Department of Justice grant recipients are not at the same time thwarting federal law enforcement priorities," a Justice Department spokesman said in praising the ruling.

Three other federal appeals courts have come to the opposite conclusion, ruling that the government was wrong to withhold the grants from sanctuary jurisdictions including San Francisco, Chicago and Philadelphia.

Federal law has long prohibited local governments from refusing to share information about the citizenship status of people they arrest or jail after convictions. Immigration agents say they need to know who in local jails is in the U.S. illegally so they can be deported after they've served their sentences.

But many cities and states have declined to provide that information or to allow immigration agents to visit their jails. They argue that to maintain the cooperation of immigrant communities, police must not be seen as extensions of federal immigration authorities.

The grant program at issue provides around $250 million a year to help fund investigative task forces, improve 911 call systems and crime laboratories, and reduce gang violence in prisons. Wednesday's ruling says the law setting up the grant program gives the attorney general authority to set the conditions for qualifying to receive the money.

While the Trump administration, beginning under former Attorney General Jeff Sessions, accelerated the fight over federal aid to sanctuary cities, the lack of cooperation with immigration agents has been a subject of bipartisan concern. The Obama administration said cities and states must comply with the law about information sharing to qualify for the grants.

The crackdown under Sessions, the 2nd Circuit said, put into effect "the same condition announced by DOJ under the preceding, Democratic administration." But the Trump administration's tougher line on immigration has sparked several legal battles, still ongoing, over the obligation of cities and states to cooperate.