如果你的Asp.Net程序執行時碰到這種錯誤:“驗證視圖狀態 MAC 失敗。如果此應用程序由網絡場或群集承載,請確保 <machineKey> 配置指定了相同的 validationKey 和驗證算法。不能在群集中使用 AutoGenerate。”那么說明你沒有讓你的應用程序使用統一的machineKey。
machineKey用途:
Asp.Net的很多加密,都是依賴于machineKey里面的值,例如Forms 身份驗證 Cookie、ViewState的加密。默認情況下,Asp.Net的配置是自己動態生成,如果單臺服務器當然沒問題,但是如果多臺服務器負載均衡,machineKey還采用動態生成的方式,每臺服務器上的machinekey值不一致,就導致加密出來的結果也不一致,不能共享驗證和ViewState,所以對于多臺服務器負載均衡的情況,一定要在每臺站點配置相同的machineKey。
machineKey生成方式: string Str = String.Format("<machineKey validation=/"3DES/" validationKey=/"{0}/" decryptionKey=/"{1}/" decryption=/"3DES/"/>",CreateKey(20),CreateKey(24)); PRotected string CreateKey(int len) { byte[] bytes = new byte[len]; new RNGCryptoServiceProvider().GetBytes(bytes); StringBuilder sb = new StringBuilder(); for(int i = 0; i < bytes.Length; i++) { sb.Append(string.Format("{0:X2}",bytes[i])); } return sb.ToString(); }
自動生成工具:http://aspnetresources.com/tools/machineKey
新聞熱點
疑難解答