【问题标题】:How to make the AntiforgeryToken As Secure in ASP.net MVC如何在 ASP.net MVC 中使 AntiforgeryToken 安全
【发布时间】:2018-05-27 15:12:28
【问题描述】:

最近我有一个要求,要制作由 ASP.net MVC 创建的 Antiforgerytoken 以使其安全

基本上防伪令牌是浏览器中名为 _RequestVerificationToken 的 cookie,希望使其安全

我已经试过了

<compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <authentication mode="Windows" />
    <identity impersonate="false" />
    <pages controlRenderingCompatibilityVersion="4.0" />
<httpCookies requireSSL="true" httpOnlyCookies="true"/>

但它并没有使令牌变得安全和 httpOnly

如果有人可以帮忙,请

将整个网站也托管在 Https 上

【问题讨论】:

  • 您的网站是否托管在 https 上?
  • 是的,我在 https 上做到了
  • 为什么要让 AntiforgeryToken 变得安全?你这是什么意思?
  • AntiforgeryToken 基本上是一个 cookie,名称为 _RequestVerificationToken ,我们必须将此 cookie 设置为浏览器中的安全 cookie
  • 如果存在安全问题,请购买更高级别的 SSL 证书。 Antiforgerytoken 已经加密。将 cookie 视为公钥,如果有人看到/得到它并不一定重要。当您使用 @Html.AntiForgeryToken() 时,会创建一个隐藏的表单字段,并用于解密 cookie。它已经很安全了。如果您的老板认为加密 cookie 会有所帮助,他似乎并不了解防伪令牌。

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

防伪令牌可用于帮助保护您的应用免受跨站点请求伪造。要使用此功能,您只需将以下 HTML 帮助程序添加到您的表单中,以便将其作为表单帖子的一部分提交。

它用于生产您的表单而不是整个 mvc 应用程序。

例如,如果您想保护您的表单。

查看:

@using(Html.BeginForm())
{
      @Html.AntiForgeryToken()
      @Html.TextBox("Name")
      <input type="submit" value="Submit"/>
}

控制器:

[HttpPost] 
[ValidateAntiForgeryToken()] 
public ActionResult PostMethod(FormCollection collection) 
{
   //Logic
}

【讨论】:

  • 我知道如何实现 Anti forgery 令牌,但基本上 AntiforgeryToken 是一个 cookie 并希望使其在浏览器端安全
  • 你能加密令牌吗?
【解决方案2】:

如果您的安全经理有供应商报告说此 cookie 不安全,请尝试将其添加到 Startup.cs ConfigureServices

services.AddAntiforgery(options =>
{
    options.FormFieldName = "AntiforgeryFieldname";
    options.HeaderName = "X-CSRF-TOKEN-HEADERNAME";
    options.SuppressXFrameOptionsHeader = false;
    options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always;
});

参考文献 https://docs.microsoft.com/en-us/aspnet/core/security/anti-request-forgery?view=aspnetcore-3.1 & https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.cookiesecurepolicy?view=aspnetcore-3.1

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 1970-01-01
    • 2010-11-23
    • 2013-01-06
    • 2013-07-22
    • 2013-02-05
    • 2015-12-13
    相关资源
    最近更新 更多