If A and B are in a contract, with B liable to A, and a third party C
takes over the liability of B to A without the consent or involvement
of B, is the contract between A and C legal?
A third party C can offer to cover the debt of B in an agreement by entering into a guarantee agreement with A (unless A forgives the debt owed by B), but that does not relieve B of liability to A, and does not constitute an agreement of B to assign the obligation to C. Usually, a guarantee agreement is valid even in the absence of consideration paid to the guarantor.
A replacement of one debtor with another with the consent of all involved is usually called a "novation".
Absent some rather involved circumstances, B has no right to complain that someone else is gratuitously agreeing to cover their debt.