Can I install a machine key for the Azure worker role?

I hosted the Owin WebAPI server as a working Azure.
Through Owin Authentication middleware, MachineKey is apparently used to encrypt and generate tokens. This works fine when I have only one instance of this role, but as soon as I want to use multiple instances, the tokens created by each instance are different.

This is the same problem as the web farm, Azure automatically solves this for WebRoles using the same .net Machine Key for all instances in Web.config.

But this does not work for worker role instances.

Is there a trick for Azure to use the same machine key for all joins of a working role?

It seems to be easier than rewriting the code to create tokens for Owin.

+4
source share
1 answer

If your self-contained application can reference System.Webyou, you can use the same MachineKey that it Microsoft.Owin.Host.SystemWebdoes.

Put the settings configuration/system.web/machineKeyin your App.config in the same way as in the Web.config file.

Link System.Weband add the following class:

public class MachineKeyDataProtector : IDataProtector
{
    private readonly string[] purposes;

    public MachineKeyDataProtector(params string[] purposes)
    {
        this.purposes = purposes;
    }

    public byte[] Protect(byte[] userData)
    {
        return MachineKey.Protect(userData, this.purposes);
    }

    public byte[] Unprotect(byte[] protectedData)
    {
        return MachineKey.Unprotect(protectedData, this.purposes);
    }
}

Then set the authentication options using this class:

        var authenticationOptions = new OAuthBearerAuthenticationOptions
                                    {
                                        AccessTokenFormat = new TicketDataFormat(new MachineKeyDataProtector(
                                            typeof(OAuthBearerAuthenticationMiddleware).Namespace, "Access_Token", "v1")),
                                        AccessTokenProvider = new AuthenticationTokenProvider(),
                                    };
+3
source

All Articles