如何在MVC中生成反xsrf令牌? [英] How to generate anti xsrf token in MVC?

查看:89
本文介绍了如何在MVC中生成反xsrf令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成防伪令牌,并检查每个页面和每个操作以检查和验证令牌但不重新生成令牌。令牌每次都不变。



我尝试过:



i在每个.cshtml页面试过

put @ Html.AntiForgeryToken()



例子

在.cshtml页面下面的代码

 @ Html.AntiForgeryToken(); 
@using(Html.BeginForm(ViewSearchAcknowledgementCase,Manan,FormMethod.Post,new {@id =Family,role =form}))
{

< table id =ContentPlaceHolder1_tbldetailcellspacing =15width =97%>
< tr align =right>
< td colspan =4>
< span id =ContentPlaceHolder1_lblmandatorystyle =color:Red;> *必填字段!< / span>
< / td>
< / tr>





----------------------- --------------------



和我检查的每一种行动方法

[ValidateAntiForgeryToken]



和下面的动作方法



 [ValidateAntiForgeryToken] 
[HttpPost]
public ActionResult ViewAcknowledgement()
{
M_Login ml = new M_Login();
尝试
{

解决方案

防伪令牌需要里面表格。

 @using(Html.BeginForm(...)
{
@ Html.AntiForgeryToken();


I want to generate an anti-forgery token and it is checked every pages and every action to check and validate token but not regenerate token. the token is every time constant.

What I have tried:

i tried in every .cshtml page
put @Html.AntiForgeryToken()

example
in .cshtml page below code

@Html.AntiForgeryToken();
               @using (Html.BeginForm("ViewSearchAcknowledgementCase", "Manan", FormMethod.Post, new { @id = "Family", role = "form" }))
               {

                   <table id="ContentPlaceHolder1_tbldetail" cellspacing="15" width="97%">
                       <tr align="right">
                           <td colspan="4">
                               <span id="ContentPlaceHolder1_lblmandatory" style="color:Red;">*  Mandatory Field!</span>
                           </td>
                       </tr>



-------------------------------------------

and every action method i checked
[ValidateAntiForgeryToken]

and action method like below

[ValidateAntiForgeryToken]
     [HttpPost]
       public ActionResult ViewAcknowledgement()
       {
           M_Login ml = new M_Login();
           try
           {

解决方案

The anti-forgery token needs to be inside the form.

@using (Html.BeginForm(...)
{
    @Html.AntiForgeryToken();


这篇关于如何在MVC中生成反xsrf令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆