A former Texas deputy has been indicted for raping a woman in her home while he was on duty, prosecutors said.

Richard Cornejo, 37, was arrested Thursday on sexual assault and official oppression charges for a July 2018 incident while he was working and driving a marked patrol vehicle, Harris County District Attorney Kim Ogg said.

“He had a duty to protect the public,” Ogg said in a statement. “He instead used his badge to commit the crime of rape.”

Prosecutors allege Cornejo, who was assigned to the Harris County Precinct 4 Constable’s Office since 2016, found a woman snoozing in a car outside an after-hours club in Houston.

The deputy woke her, and offered to follow her home, where the alleged assault took place, she told police.

Cornejo was “immediately removed” from patrol duties after Houston police contacted his supervisors in November about the woman’s allegations, department officials said in a statement to KTRK.

Cornejo — who was fired that month for violating department policies — admitted he had sex with the woman, but claimed it was consensual.

The former deputy now faces up to 21 years in prison if convicted on all charges, Ogg said. He remained in custody Friday on $105,000 bond, jail records show. It’s unclear if he has hired an attorney.

The after-hours club where Cornejo allegedly found the woman was later closed due to several drug and liquor violations, prosecutors said.