是否有可能设置的machineKey为Azure的工作者角色 [英] Is it possible to set a machinekey for an Azure Worker Role

查看:347
本文介绍了是否有可能设置的machineKey为Azure的工作者角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主持在Azure的辅助角色的Owin的WebAPI服务器。结果
所述Owin认证中间件似乎使用将machineKey加密和生成令牌。
这完美的作品时,我有这个角色只有一个实例,但只要我想用几个实例,每个实例生成的令牌型动物。

I have hosted an Owin WebAPI Server in an Azure Worker Role.
The Owin Authentication middleware seems to use the MachineKey to encrypt and generate Tokens. This works perfectly when I have only one instance of this role, but as soon as I want to use several instances, the tokens generated by each instance are differents.

这是同样的问题,作为一个Web场,天青自动使用Web.config文件中的所有实例相同的.NET机主要解决了这个为WebRoles。

This is the same problem as a 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.

有没有窍门有天青使用同一台机器键辅助角色的所有intsances?

Is there a trick to have Azure using the same machine key for all the intsances of a worker Role ?

似乎会比改写code生成Owin的标记更容易。

Seems it would be easier than rewriting code to generate the tokens for Owin.

推荐答案

如果你的自我托管的应用程序可以参考的System.Web ,那么你可以使用相同的<一个href=\"https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Web.Security.MachineKey);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5.1);k(DevLang-csharp)&rd=true\"相对=nofollow>的machineKey implementaiton的 Microsoft.Owin.Host.SystemWeb 一样。

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

配置/ System.Web程序/ machineKey中在你的App.config设置,就像它是在Web.config。

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

参考参考的System.Web 并添加以下类:

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(),
                                    };

这篇关于是否有可能设置的machineKey为Azure的工作者角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆