MVC: Generating an AntiForgeryToken for a AJAX response to the client

ぐ巨炮叔叔 提交于 2019-12-24 01:37:57

问题


I've spent a good few hours tonight trying to work this one out. Basically I have a form in MVC with an AntiForgeryToken so when I post back everything is validated and it's all good. The problem is that I have a JSON result method that generates a load of markup and returns HTML to the user. I am regenerating forms to return to the user in an ajax way, so I need to put the token into the HTML I return to the client otherwise when they POST back again the antiforgery stuff will still work.

So the essence of my problem is how would I access the token in order to put it in my dynamically generated html server side and how would I generate the matching cookie and then send it all back in the response to the user? Can't seem to find anything on google about this but people must be doing it. Help would be very much appreciated as my brain is starting to shutdown! Thanks This is the form

<form action="/DoSomething" method="post">
    <input name="__RequestVerificationToken" type="hidden" value="saTFWpkKN0BYazFtN6c4YbZAmsEwG0srqlUqqloi/fVgeV2ciIFVmelvzwRZpArs" />
    <!-- rest of form goes here -->
</form>

So in my controller i am building tis form with a stringbuilder to send back to the client in a JSON result but i need to be able to add the value for the __RequestVerificationToken to the input so that my security will still work

来源:https://stackoverflow.com/questions/6656999/mvc-generating-an-antiforgerytoken-for-a-ajax-response-to-the-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!