What is the basis for saying that Aquinas accepted the Immaculate Conception? It's commonly said that Thomas Aquinas did not accept the doctrine of the immaculate conception of Mary.  For example, Wikipedia says:


  Saint Thomas Aquinas refused to concede the Immaculate Conception, on the ground that, unless the Blessed Virgin had at one time or other been one of the sinful, she could not justly be said to have been redeemed by Christ.


The article cites the Summa Theologica, III, Q27, A2, which argues that Mary was sanctified after animation, not before:


  And thus, in whatever manner the Blessed Virgin would have been sanctified before animation, she could never have incurred the stain of original sin: and thus she would not have needed redemption and salvation which is by Christ, of whom it is written (Matthew 1:21): "He shall save His people from their sins." But this is unfitting, through implying that Christ is not the "Saviour of all men," as He is called (1 Timothy 4:10). It remains, therefore, that the Blessed Virgin was sanctified after animation.


But recently, @Geremia made me aware that there is good reason to believe that Aquinas actually did support the immaculate conception of Mary in other writings.

What is the basis for saying that Aquinas did, at some point in his life, believe in the immaculate conception?
St. Thomas's views on the Immaculate Conception went through three phases.cf. also ch. 2, art. 2, § "St. Thomas and the Immaculate Conception" of Mother of the Saviour and Our Interior Life by Fr. Réginald Garrigou-Lagrange, O.P.

Phase 1: clear support of it

St. Thomas's clearest support of the Immaculate Conception is in his commentary (1252-6) on Peter Lombard's Sentences (Super Sent., lib. 1 d. 44 q. 1 a. 3 ad 3):


  puritas intenditur per recessum a contrario: et ideo potest aliquid creatum inveniri quo nihil purius esse potest in rebus creatis, si nulla contagione peccati inquinatum sit; et talis fuit puritas beatae virginis, quae a peccato originali et actuali immunis fuit.Purity is increased by withdrawing from its opposite: hence there can be a creature than whom no more pure is possible in creation, if it be free from all contagion of sin: and such was the purity of the Blessed Virgin who was immune from original and actual sin.


Phase 2: grappling with it

Summa Theologica III, written in 1272-3, contains the famous question 27 on the Sanctification of the Blessed Virgin, in which he seems to deny the Immaculate Conception.

Phase 3: return to his original position

Explanation of the Lord's Prayer, petition 5 (Lent 1273):


  …beatae virgini, quae fuit plena gratiae, in qua nullum peccatum fuit.…the Blessed Virgin, who was full of grace, in whom there was no sin.


Commentary on Psalm 18 (1272-3):


  …beata virgine, quae nullam habuit obscuritatem peccati.…the Blessed Virgin, who had no darkness of sin.


He preached in his On the Angelic Salutation (Lent 1273):


  Ipsa (Virgo) omne peccatum vitavit magis quam alius sanctus, praeter Christum. Peccatum enim aut est originale, et de isto fuit mundata in utero; aut mortale aut veniale, et de istis libera fuit. … Sed Christus excellit beatam virginem in hoc quod sine originali conceptus et natus est.
  Beata autem virgo in originali est concepta, sed non nata.For She Herself avoided every sin, more holy than anyone after Christ. For sin is either original, and from this she was cleansed in the womb; or mortal or venial, and from these She was free. … But Christ excelled the Blessed Virgin in this, that He was conceived and born without original (sin). Moreover the the Blessed Virgin was conceived in original (sin), but not born (in it).


The sense of "the Blessed Virgin was conceived in/with original sin (in/cum peccato originali)" is explained by a similar passage of his Compendium Theologiæ cap. 224 ("Sanctification of Christ's Mother") (1272-3), in which he seems to be referring to Sts. Anne's and Joachim's sexual act,* not to the Blessed Virgin's participation in Adam's sin (or lack thereof):*(cf. St. Augustine's theory that the propagation of original sin is due to the concupiscence inherent in the sexual act after the Fall, On Marriage &amp; Concupiscence ch. 27, and this)


  Nec solum a peccato actuali immunis fuit, sed etiam ab originali, speciali privilegio mundata. Oportuit siquidem quod cum peccato originali conciperetur, utpote quae ex utriusque sexus commixtione concepta fuit. Hoc enim privilegium sibi soli servabatur ut virgo conciperet filium Dei. Commixtio autem sexus, quae sine libidine esse non potest post peccatum primi parentis, transmittit peccatum originale in prolem. Similiter etiam quia si cum peccato originali concepta non fuisset, non indigeret per Christum redimi, et sic non esset Christus universalis hominum redemptor, quod derogat dignitati Christi. Est ergo tenendum, quod cum peccato originali concepta fuit, sed ab eo quodam speciali modo purgata fuit.Mary was not only free from actual sin, but she was also, by a special privilege, cleansed from original sin. She had, indeed, to be conceived with original sin, inasmuch as her conception resulted from the commingling of both sexes. For the privilege of conceiving without impairment of virginity was reserved exclusively to her who as a virgin conceived the Son of God. But the commingling of the sexes which, after the sin of our first parent, cannot take place without lust,* transmits original sin to the offspring. Likewise, if Mary had been conceived without original sin, she would not have had to be redeemed by Christ, and so Christ would not be the universal redeemer of men, which detracts from His dignity. Accordingly we must hold that she was conceived with original sin, but was cleansed from it in some special way.


*(cf. Psalm 50:7: "For behold I was conceived in iniquities; and in sins did my mother conceive me.")

See:  


Pedro Lumbreras O.P., "Saint Thomas and the Immaculate Conception," Homiletic and Pastoral Review 24, no. 3 (1923): 253-63.&nbsp;&nbsp;&nbsp;&nbsp;• Book-length response: Hugolinus Storff, O.F.M., The Immaculate Conception: The Teaching of St. Thomas, St. Bonaventure and Bl. J. Duns Scotus on the Immaculate Conception of the Blessed Virgin Mary (San Francisco, Calif.: St. Francis Press, 1925).
Norbert del Prado O.P., Divus Thomas et bulla dogmatica "Ineffabilis Deus" (Friburgi Helvetiorum: Ex typis Consociationis sancti Pauli, 1919).