Aran Mulholland
Aran Mulholland

Reputation: 23935

If I use the auto scaling features of Azure Web Sites do I need to set a specific machine key?

If you use auto scaling with Azure Web Sites do you need to set the machine key so encrypted authentication tokens can be shared between machines?

There is a question here which appears to be the same as the one I am asking. However that question refers to Azure Web Roles. I am asking about Azure Web Sites.

Upvotes: 2

Views: 982

Answers (1)

ahmelsayed
ahmelsayed

Reputation: 7392

No you don't need to. Azure Website will set the same machine key for all your instances when they are running on 2 (or 10) different VMs.

If you want a quick and dirty way to verify this, have the following bit of code in your Application_Start() basically this writes the machine key into a file called %WEBSITE_INSTANCE_ID% this is a unique environment variable per instance. Scale to 2 machines, turn on Always On setting and within a minute 2 files should be written in your D:\home\site\wwwroot folder that have a long guid for names (the instance id for the 2 machines) and they will contain the same key.

code credit goes to this

protected void Application_Start()
{
    var section = (MachineKeySection)
        ConfigurationManager.GetSection("system.web/machineKey");

    BindingFlags flags =
        BindingFlags.Instance |
        BindingFlags.NonPublic |
        BindingFlags.GetProperty;

    Func<string, byte[]> propertyReader = name => (byte[])section
        .GetType()
        .GetProperty(name, flags)
        .GetValue(section, null);

     using (
        var writer =
            new StreamWriter(Environment.ExpandEnvironmentVariables(@"%HOME%\site\wwwroot\%WEBSITE_INSTANCE_ID%.log")))
    {
        var key = ConvertToHex(
            propertyReader("DecryptionKeyInternal"));
        writer.WriteLine("DecryptKey: {0}", key);

        var iv = ConvertToHex(
            propertyReader("ValidationKeyInternal"));
        writer.WriteLine("ValidationKey: {0}", iv);
    }

}

private string ConvertToHex(byte[] binary)
{
    return binary.Aggregate(
        new StringBuilder(),
        (acc, c) => acc.AppendFormat("{0:x2}", c),
        acc => acc.ToString());
}

Upvotes: 8

Related Questions