Asp.net
AutoValidateAntiForgeryToken 與 ValidateAntiForgeryToken
我剛才試圖通過提供防偽令牌來保護 post 方法免受側面腳本的影響,但注意到,在 .Net Core 中有另一個名為
AutoAntiForgeryToken. XML 註釋和線上搜尋沒有提供關於這個新屬性的太多資訊。任何有關新屬性的幫助和描述都將不勝感激。
從
AutoValidateAntiforgeryTokenAttribute文件:導致驗證所有不安全 HTTP 方法的防偽令牌的屬性。GET、HEAD、OPTIONS 和 TRACE 以外的 HTTP 方法需要防偽令牌。預設情況下,它可以用作全域過濾器來觸發對應用程序的防偽令牌的驗證。
AutoValidateAntiforgeryTokenAttribute允許將防偽令牌驗證全域應用於所有不安全的方法,例如POST, PUT, PATCH and DELETE. 因此,您不需要[ValidateAntiForgeryToken]為每個需要它的操作添加屬性。要使用它,請將以下程式碼添加到您的類
ConfigureServices方法中Startupservices.AddMvc(options => { options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); });如果您需要忽略防偽驗證,您可以
[IgnoreAntiforgeryToken]向操作添加屬性。