CA5395:遺漏動作方法的 HttpVerb 屬性

屬性
規則識別碼 CA5395
標題 遺漏動作方法的 HttpVerb 屬性
類別 安全性
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 No

原因

未明確指定動作方法的 HTTP 要求種類。

檔案描述

建立、編輯、刪除或修改資料的所有動作方法,都必須受到防偽屬性的保護,以防止跨網站偽造要求攻擊。 執行 GET 作業應該是安全的作業,沒有任何副作用,而且不會修改您的保存資料。

如何修正違規

使用 HttpVerb 屬性標記動作方法。

隱藏警告的時機

如果下列狀況,可以安全地隱藏來自此規則的警告:

隱藏警告

如果您只想要隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。

#pragma warning disable CA5395
// The code that's violating the rule is on this line.
#pragma warning restore CA5395

若要停用檔案、資料夾或項目的規則,請在組態檔中將其嚴重性設定為 。none

[*.{cs,vb}]
dotnet_diagnostic.CA5395.severity = none

如需詳細資訊,請參閱 如何隱藏程式代碼分析警告

虛擬程式代碼範例

違規

using Microsoft.AspNetCore.Mvc;

[ValidateAntiForgeryToken]
class BlahController : Controller
{
}

class ExampleController : Controller
{
    public IActionResult ExampleAction()
    {
        return null;
    }
}

解決方案

using Microsoft.AspNetCore.Mvc;

[ValidateAntiForgeryToken]
class BlahController : Controller
{
}

class ExampleController : Controller
{
    [HttpGet]
    public IActionResult ExampleAction()
    {
        return null;
    }
}