President Trump signed an executive order on Friday that purports to bar for at least 90 days almost all permanent immigration from seven majority-Muslim countries, including Syria and Iraq, and asserts the power to extend the ban indefinitely.

But the order is illegal. More than 50 years ago, Congress outlawed such discrimination against immigrants based on national origin.

That decision came after a long and shameful history in this country of barring immigrants based on where they came from. Starting in the late 19th century, laws excluded all Chinese, almost all Japanese, then all Asians in the so-called Asiatic Barred Zone. Finally, in 1924, Congress created a comprehensive “national-origins system,” skewing immigration quotas to benefit Western Europeans and to exclude most Eastern Europeans, almost all Asians, and Africans.

Mr. Trump appears to want to reinstate a new type of Asiatic Barred Zone by executive order, but there is just one problem: The Immigration and Nationality Act of 1965 banned all discrimination against immigrants on the basis of national origin, replacing the old prejudicial system and giving each country an equal shot at the quotas. In signing the new law, President Lyndon B. Johnson said that “the harsh injustice” of the national-origins quota system had been “abolished.”

Nonetheless, Mr. Trump asserts that he still has the power to discriminate, pointing to a 1952 law that allows the president the ability to “suspend the entry” of “any class of aliens” that he finds are detrimental to the interest of the United States.

But the president ignores the fact that Congress then restricted this power in 1965, stating plainly that no person could be “discriminated against in the issuance of an immigrant visa because of the person’s race, sex, nationality, place of birth or place of residence.” The only exceptions are those provided for by Congress (such as the preference for Cuban asylum seekers).

When Congress passed the 1965 law, it wished to protect not just immigrants, but also American citizens, who should have the right to sponsor their family members or to marry a foreign-born spouse without being subject to pointless discrimination.

Mr. Trump may want to revive discrimination based on national origin by asserting a distinction between “the issuance of a visa” and the “entry” of the immigrant. But this is nonsense. Immigrants cannot legally be issued a visa if they are barred from entry. Thus, all orders under the 1952 law apply equally to entry and visa issuance, as his executive order acknowledges.

Note that the discrimination ban applies only to immigrants. Legally speaking, immigrants are those who are given permanent United States residency. By contrast, temporary visitors like guest workers, students and tourists, as well as refugees, could still be barred. The 1965 law does not ban discrimination based on religion — which was Mr. Trump’s original proposal.

While presidents have used their power dozens of times to keep out certain groups of foreigners under the 1952 law, no president has ever barred an entire nationality of immigrants without exception. In the most commonly cited case, President Jimmy Carter barred certain Iranians during the 1980 hostage crisis, but the targets were mainly students, tourists and temporary visitors. Even then, the policy had many humanitarian exceptions. Immigrants continued to be admitted in 1980.

While courts rarely interfere in immigration matters, they have affirmed the discrimination ban. In the 1990s, for example, the government created a policy that required Vietnamese who had fled to Hong Kong to return to Vietnam if they wanted to apply for United States immigrant visas, while it allowed applicants from other countries to apply for visas wherever they wanted. A federal appeals court blocked the policy.

The government in that case did not even bother arguing that the 1952 law permitted discrimination. The court rejected its defense that a “rational link” with a temporary foreign policy measure could justify ignoring the law — an argument the Trump administration is sure to make. The court wrote, “We cannot rewrite a statutory provision which by its own terms provides no exceptions or qualifications.”

To resolve this case, Congress amended the law in 1996 to state that “procedures” and “locations” for processing immigration applications cannot count as discrimination. While there is plenty of room for executive mischief there, the amendment made clear that Congress still wanted the discrimination ban to hold some force. A blanket immigration prohibition on a nationality by the president would still be illegal.

Even if courts do find wiggle room here, discretion can be taken too far. If Mr. Trump can legally ban an entire region of the world, he would render Congress’s vision of unbiased legal immigration a dead letter. An appeals court stopped President Barack Obama’s executive actions to spare millions of undocumented immigrants from deportations for the similar reason that he was circumventing Congress. Some discretion? Sure. Discretion to rewrite the law? Not in America’s constitutional system.