Is it possible to set a machinekey for an Azure Worker Role

倾然丶 夕夏残阳落幕 提交于 2019-12-05 17:55:26

If your self-hosted application can reference System.Web, then you can use the same MachineKey implementaiton that the Microsoft.Owin.Host.SystemWeb does.

Put the configuration/system.web/machineKey settings in your App.config just like it is in the Web.config.

Reference reference System.Web and 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 your authentication options using that class:

        var authenticationOptions = new OAuthBearerAuthenticationOptions
                                    {
                                        AccessTokenFormat = new TicketDataFormat(new MachineKeyDataProtector(
                                            typeof(OAuthBearerAuthenticationMiddleware).Namespace, "Access_Token", "v1")),
                                        AccessTokenProvider = new AuthenticationTokenProvider(),
                                    };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!