There are two constitutional doctrines that prohibit discrimination based upon state of residence.
One is the dormant commerce clause (a court created doctrine) and the other is the privileges and immunities clause. There are actually two privileges and immunities clauses, one in Article IV, Section 2, Clause 1, which states:
The Citizens of each State shall be entitled to all Privileges and
Immunities of Citizens in the several States.
and the other in Section 1 of the 14th Amendment which states:
All persons born or naturalized in the United States, and subject to
the jurisdiction thereof, are citizens of the United States and of the
state wherein they reside. No state shall make or enforce any law
which shall abridge the privileges or immunities of citizens of the
United States; nor shall any state deprive any person of life,
liberty, or property, without due process of law; nor deny to any
person within its jurisdiction the equal protection of the laws.
Some of the leading privilege and immunities cases are reviewed here.
With respect to the dormant commerce clause, per the link above:
[I]n a dormant Commerce Clause case, a court is initially concerned
with whether the law facially discriminates against out-of-state
actors or has the effect of favoring in-state economic interests over
out-of-state interests. Discriminatory laws motivated by "simple
economic protectionism" are subject to a "virtually per se rule of
invalidity," City of Philadelphia v. New Jersey, 437 U.S. 617
(1978), Dean Milk Co. v. City of Madison, Wisconsin, 340 U.S. 349
(1951), Hunt v. Washington State Apple Advertising Comm., 432 U.S.
333 (1977) which can only be overcome by a showing that the State has
no other means to advance a legitimate local purpose, Maine v.
Taylor, 477 U.S. 131(1986). See also Brown-Forman Distillers v. New
York State Liquor Authority, 476 U.S. 573 (1986).
Notwithstanding these constitutional protections against discrimination based upon state residence, the U.S. Supreme Court held that this discrimination is constitutional in the case Vlandis v. Kline, 412 U.S. 441 (1973) and reaffirmed the continuing vitality of that decision in Saenz v. Roe, 526 U.S. 489 (1999). The majority opinion in Vlandis v. Kline explains this authority was basically undisputed at that time:
The appellees do not challenge, nor did the District Court invalidate,
the option of the State to classify students as resident and
nonresident students, thereby obligating nonresident students to pay
higher tuition and fees than do bona fide residents. The State's right
to make such a classification is unquestioned here.
Some of the justification is set forth in the Saenz v. Roe (1999) decision which states:
The second component of the right to travel is, however, expressly
protected by the text of the Constitution. The first sentence of
Article IV, § 2, provides: "The Citizens of each State shall be
entitled to all Privileges and Immunities of Citizens in the several
States." Thus, by virtue of a person's state citizenship, a citizen of
one State who travels in other States, intending to return home at the
end of his journey, is entitled to enjoy the "Privileges and
Immunities of Citizens in the several States" that he visits. This
provision removes "from the citizens of each State the disabilities of
alienage in the other States." It provides important protections for
nonresidents who enter a State whether to obtain employment, to
procure medical services, or even to engage in commercial shrimp
fishing, Toomer v. Witsell, 334 U.S. 385 (1948). Those protections are
not "absolute," but the Clause "does bar discrimination against
citizens of other States where there is no substantial reason for the
discrimination beyond the mere fact that they are citizens of other
States." There may be a substantial reason for requiring the
nonresident to pay more than the resident for a hunting license, see
Baldwin v. Fish and Game Comm'n of Mont., 436 U.S. 371 (1978), or to
enroll in the state university, see Vlandis v. Kline, 412 U.S. 441
(1973), but our cases have not identified any acceptable reason for
qualifying the protection afforded by the Clause for "the 'citizen of
State A who ventures into State B' to settle there and establish a
home." Zobel, 457 U.S. at 74 (O'CONNOR, J., concurring in judgment).
Permissible justifications for discrimination between residents and
nonresidents are simply inapplicable to a nonresident's exercise of
the right to move into another State and become a resident of that
State.
Despite fundamentally differing views concerning the coverage of the
Privileges or Immunities Clause of the Fourteenth Amendment, most
notably expressed in the majority and dissenting opinions in the
Slaughter-House Cases (1873), it has always been common ground that
this Clause protects the third component of the right to travel.
Writing for the majority in the Slaughter-House Cases, Justice Miller
explained that one of the privileges conferred by this Clause "is that
a citizen of the United States can, of his own volition, become a
citizen of any State of the Union by a bona fide residence therein,
with the same rights as other citizens of that State."
Basically, the exception isn't logical and instead is a case of the Courts upholding a common practice that seems to make economic sense, notwithstanding that fact that it is clearly contrary to the relevant constitutional provisions and doctrines. The life of the law is not reason, it is experience. Assuming logical consistency in the law will often lead you to a descriptively inaccurate result.