我正在运行Azure网站。每当我部署时,每个人都会注销,因为machineKey更改。
我在web.config中指定了machineKey,但这并没有解决问题。我相信这是因为Azure自动覆盖machineKey [1]。
解决方法
尝试在Application_Start上重置机器密钥配置部分:
protected void Application_Start() { // ... var mksType = typeof(MachineKeySection); var mksSection = ConfigurationManager.GetSection("system.web/machineKey") as MachineKeySection; var resetMethod = mksType.GetMethod("Reset",BindingFlags.NonPublic | BindingFlags.Instance); var newConfig = new MachineKeySection(); newConfig.ApplicationName = mksSection.ApplicationName; newConfig.CompatibilityMode = mksSection.CompatibilityMode; newConfig.DataProtectorType = mksSection.DataProtectorType; newConfig.Validation = mksSection.Validation; newConfig.ValidationKey = ConfigurationManager.AppSettings["MK_ValidationKey"]; newConfig.DecryptionKey = ConfigurationManager.AppSettings["MK_DecryptionKey"]; newConfig.Decryption = ConfigurationManager.AppSettings["MK_Decryption"]; // default: AES newConfig.ValidationAlgorithm = ConfigurationManager.AppSettings["MK_ValidationAlgorithm"]; // default: SHA1 resetMethod.Invoke(mksSection,new object[] { newConfig }); }
以上假设您在< appSettings>中设置了适当的值部分:
<appSettings> <add key="MK_ValidationKey" value="...08EB13BEC0E42B3F0F06B2C319B..." /> <add key="MK_DecryptionKey" value="...BB72FCE34A7B913DFC414E86BB5..." /> <add key="MK_Decryption" value="AES" /> <add key="MK_ValidationAlgorithm" value="SHA1" /> </appSettings>
但是您可以从您喜欢的任何配置源加载实际值。