如何在 Azure 网站上设置 machineKey [英] How to set machineKey on Azure Website
问题描述
我正在运行 Azure 网站.每当我部署时,每个人都会因为 machineKey
更改而注销.
I'm running an Azure Website. Whenever I deploy, everyone gets logged out because the machineKey
changes.
我在 web.config
中指定了 machineKey
但这并没有解决问题.我相信这是因为 Azure 会自动覆盖 machineKey
[1].
I specified the machineKey
in the web.config
but this didn't solve the issue. I believe this is because Azure automatically overwrites the machineKey
[1].
我在这里发现了几个类似的问题,但答案链接到死链接.
I've found a couple of similar questions here but the answers link to dead links.
那么,解决方案是什么?无论 Azure 上的部署如何,肯定有一种方法可以让用户保持登录状态.
So, what's the solution? Surely there's a way to keep users logged in regardless of deployments on Azure.
推荐答案
尝试在 Application_Start
时重置机器密钥配置部分:
Try to reset the machine-key configuration section upon 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 });
}
以上假设您在
部分设置了适当的值:
The above assumes you set the appropriate values in the <appSettings>
section:
<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>
但是您可以从您喜欢的任何配置源加载您的实际值.
But you can load your actual values from any configuration source you like.
这篇关于如何在 Azure 网站上设置 machineKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!