A former Eagan middle school teacher will serve 30 days of house arrest in each of the next five years after pleading guilty to stalking a former 12-year-old female student on social media, the Dakota County attorney's office announced Tuesday.

Cody Woodrow Hansen, 26, of Hastings, was sentenced Thursday and also will serve five years probation. Hansen pleaded guilty in August to using Snapchat to interact with the former student for several months.

"We are pleased to have held this former teacher accountable for this troubling inappropriate behavior which is a crime under Minnesota law," County Attorney James Backstrom said in a news release.

Sarah MacGillis, Hansen's attorney, said prosecutors sought 90 days in custody, but the judge rejected the sentence in favor of probation.

"We are pleased that the judge understood Mr. Hansen's clear expression of remorse and sentenced him accordingly," MacGillis said by e-mail.

Last year, Hansen allegedly used several usernames to contact the student. Hansen told the student he thought she was cute, asked if she had ever kissed anyone and if she liked older men. The student didn't know the messages were coming from Hansen until he admitted who he was and that he would get into trouble.

The student told Eagan police that she "felt creepy and unsafe" in Hansen's classroom.

Karen Zamora • 612-673-4647

Twitter: @KarenAnelZamora