I have been working for some time on a paper that I hope to submit to a mathematics journal. Buried deep in the guts of the paper is a technical lemma that I struggled with for some time before finally posting the question to MSE, where it promptly received an answer. What is the proper way to acknowledge this assistance in the submitted manuscript?
In your case, the user who helped you participates under his real name (he's from my hometown :)). I would include his proof, with a footnote at the page bottom "This proof is due to ...", and additionally thank him where you usually thank people (I've most typically seen this as footnotes on the page of the abstract, but whatever is normal for you). To cover all bases, I'd finally contact Hagen by simply leaving a comment under his answer with @(name) either linking to this question, or outlining what you intend to do, and getting his sign-off to do so. I think in any case where you can identify a user by name, mention of MSE is optional.
The same generalizes for those participating not under their real names. You should then just discuss in your @(name) message under which name (if any) user would like to be quoted. If user agrees to using their proof but prefers to not be mentioned by name, then thank an anonymous user of MSE.
The proper way to acknowledge any source other than your own brain is to formally cite that source in your bibliography; whether that source is a textbook, journal paper, proceedings paper, preprint, StackExchange questions, blog post, newspaper article, movie, cereal box, bathroom wall, or your mom is utterly immaterial. If your source is a StackExchange site, then you must cite the StackExchange site.
(Full disclosure: I have cited blogs, StackExchange questions, Usenet posts, newspaper articles, movies, and video games in my refereed journal papers, and at least one of my StackExchange questions has more citations than at least one of my research papers.)