如何获得AntiForgeryToken值,而隐藏的输入 [英] How to get AntiForgeryToken value without hidden input

查看:443
本文介绍了如何获得AntiForgeryToken值,而隐藏的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@ Html.AntiForgeryToken()
呈现隐藏输入

 <输入名称=__ RequestVerificationToken类型=隐藏值=GuiNIwhIJZjINHhuS_8FenaFDXIiaE/>

我怎么能只得到象征价值?
没有难看code是这样的:

 公共静态IHtmlString AntiForgeryTokenValue(此的HtmlHelper的HtmlHelper){
        VAR字段= htmlHelper.AntiForgeryToken()ToHtmlString()。
        VAR beginIndex = field.IndexOf(值= \\)+ 7;
        VAR endIndex的= field.IndexOf(\\,beginIndex);
        返回新HtmlString(field.Substring(beginIndex,endIndex的 - beginIndex));
    }


解决方案

MVC的反CSRF能力,实际上取决于的两个的标记:一个是隐藏的表单元素,另一个是一个cookie 。所以Html.AntiForgeryToken()辅助不只是返回的HTML片段。的它也有设置cookie的副作用。的注意,cookie的值和形式的价值都没有,因为每次连接code不同的信息相同。

如果您使用AntiForgery.GetTokens API,此方法将返回原始的标记,而不是生成HTML代码段。该方法的参数是:


  • oldCookieToken:如果请求已经包含反CSRF令牌的cookie,这里提供。此参数可以为null。

  • newCookieToken(out参数):如果的 oldCookieToken 的为空或没再present一个有效的反CSRF的cookie标记,该参数将与填充值,你应该把在响应中的cookie。如果的 oldCookieToken 的再presented一个有效的反CSRF令牌,然后的 newCookieToken 的将包含null在方法返回时,你不必设置一个响应的cookie。

  • formToken(out参数):此参数将与令牌回发到服务器时,应该在表单主体present填充。这是结束了到Html.AntiForgeryToken()的调用由隐藏input元素被包裹的价值。

如果您使用此API来手动生成的Cookie和表单令牌,你需要调用的为了验证令牌AntiForgery.Validate相​​应的过载

@Html.AntiForgeryToken() renders hidden input

<input name="__RequestVerificationToken" type="hidden" value="GuiNIwhIJZjINHhuS_8FenaFDXIiaE" />

How can I get token value only? Without ugly code like this:

public static IHtmlString AntiForgeryTokenValue(this HtmlHelper htmlHelper) {
        var field = htmlHelper.AntiForgeryToken().ToHtmlString();
        var beginIndex = field.IndexOf("value=\"") + 7;
        var endIndex = field.IndexOf("\"", beginIndex);
        return new HtmlString(field.Substring(beginIndex, endIndex - beginIndex));
    }

解决方案

The anti-CSRF capabilities of MVC actually depend on two tokens: one is a hidden form element, and the other is a cookie. So the Html.AntiForgeryToken() helper doesn't just return an HTML snippet. It also has a side effect of setting this cookie. Note that the cookie value and the form value are not equal since they each encode different pieces of information.

If you use the AntiForgery.GetTokens API, this method will return the raw tokens instead of generating an HTML snippet. The parameters to this method are:

  • oldCookieToken: If the request already contains an anti-CSRF cookie token, provide it here. This parameter may be null.
  • newCookieToken (out parameter): If oldCookieToken was null or did not represent a valid anti-CSRF cookie token, this parameter will be populated with the value that you should put in the response cookie. If oldCookieToken represented a valid anti-CSRF token, then newCookieToken will contain null when the method returns, and you don't have to set a response cookie.
  • formToken (out parameter): This parameter will be populated with the token that should be present in the form body when posting back to the server. This is the value that ends up being wrapped by the hidden input element in a call to Html.AntiForgeryToken().

If you use this API to generate cookie and form tokens manually, you'll need to call the corresponding overload of AntiForgery.Validate in order to validate the tokens.

这篇关于如何获得AntiForgeryToken值,而隐藏的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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