2012-09-28 19 views
1

可能重複:
runtime loading of ValidateAntiForgeryToken Salt valueMVC3防僞造 - 如何使用鹽從配置文件

我已經添加了鹽,放入配置文件,但它拋出下面的錯誤。有沒有辦法從配置文件中使用Salf值?我試圖使用Static而不是const,但無法正常工作。

錯誤 constant initializer must be compile-time constant

代碼

public const string SaltValue= ConfigurationManager.AppSettings["SaltValue"]; 

[HttpPost] 
[ValidateAntiForgeryToken(Salt = SaltValue)] 
public ActionResult Index(SomeViewModel someViewModel) 
{ 
    return View(); 
} 

非常感謝

+0

看起來不像重複 – user1211185

回答

0

您應該能夠從ConfigurationManager.AppSettings [使用公共靜態只讀SaltValue然後分配值「SaltValue」]在控制器的構造函數中。這是兩個步驟,一個是創建字符串,另一個是填充構造函數中的值。那麼你應該可以在你的ValidateAntiForgeryToken屬性中使用它。