属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

Onl*_*ere 9 c# string asp.net-mvc-3

这是我用来调用一些助手的常量类:

public static class SecurityHelpers
{
    public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner";         
}
Run Code Online (Sandbox Code Playgroud)

以下是我在MVC3 Web应用程序中的一个表单中调用它的方法:

@using (Html.BeginForm("Index", "Checkout", FormMethod.Post))
{   
    <input type="hidden" name="amount" value="@Model.PackageCost"/>
    <input type="hidden" name="currency" value="$"/>
    <input type="hidden" name="itemdescription" value="@Model.PackageDescriptor"/>
    <input type="hidden" name="type" value="digital"/>
    @Html.AntiForgeryToken(App.WebUI.Helpers.SecurityHelpers.AntiforgeryTokenSalt)

    <input type="submit" value="Confirmar" class="btn primary frmsubmit" />
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

[HttpPost]
[ValidateAntiForgeryToken(Salt = SecurityHelpers.AntiforgeryTokenSalt)]
public ActionResult Index(decimal amount, string currency, string itemDescription, string type)
{
    if (!User.Identity.IsAuthenticated) return RedirectToAction("LogOn", "Account");
}
Run Code Online (Sandbox Code Playgroud)

错误是在我的控制器中触发的,它说:

属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

任何想法为什么这不起作用?SaltValidateAntiForgeryToken装饰器的属性是一个字符串,我的常量也是一个字符串,所以我很困惑.

Ric*_*ton 25

静态字符串不是常量.

尝试改变

public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 
Run Code Online (Sandbox Code Playgroud)

public const string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 
Run Code Online (Sandbox Code Playgroud)