2011-10-18 13 views
8

Oto stała klasa używam powołać kilka pomocników:Argument atrybut musi być stałym wyrażeniem, typeof ekspresji lub tworzenia tablicy wyrażenia typu parametru atrybut

public static class SecurityHelpers 
{ 
    public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner";   
} 

A oto jak przywołuję go w jednym z moje formularze w moim MVC3 aplikacji internetowej:

@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" /> 
} 

I w moim kontrolera:

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

Błąd jest zwolniony w moim kontrolera, to mówi:

Argument atrybut musi być stałym wyrażeniem, typeof ekspresji lub tworzenia tablicy wyrażenia typu parametru atrybut

jakieś pomysły dlaczego tak ISN działa? Atrybut ValidateAntiForgeryToken jest ciągiem o wartości Salt, a moja stała to również ciąg, więc jestem zdezorientowany.

Odpowiedz

20

Statyczny ciąg nie jest stałą.

Spróbuj zmienić

public static string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 

do

public const string AntiforgeryTokenSalt = "tokenFooYouTolkienBladeRunner"; 
Powiązane problemy