任何人都可以举出使用 Microsoft Azure 管理库 (MAML) 来扩展 Redis 缓存服务的示例吗?
我必须使用旧版本 Microsoft.Azure.Management.Redis.dll v0.9.0.0,因此 RedisManagementClient 不会接收 token ,而只会接收凭据。在这种情况下会出现异常
"AuthenticationFailed: Authentication failed. The 'Authorization' header is missing."
这是我正在使用的代码:
public static void ScaleRedis(eSubscriptionType subscriptionType)
{
RedisManagementClient client = new RedisManagementClient(AzureCredentials.GetCredentials(subscriptionType));
var redisParams = new RedisCreateOrUpdateParameters()
{
Properties = new RedisProperties(version, new Sku(redisSKUName, redisSKUFamily, redisSKUCapacity), false),
Location = redisCacheRegion
};
client.Redis.CreateOrUpdate(resourceGroupName, cacheName, redisParams);
}
最佳答案
I must use older version Microsoft.Azure.Management.Redis.dll, v0.9.0.0, and so the RedisManagementClient do not receive token, but only credentials.
据我所知,名为 Microsoft.Azure.*
的库用于调用 ARM REST API与 TokenCloudCredentials ,而 Microsoft.WindowsAzure.*
可以与 CertificateCloudCredentials 一起使用.
如果您使用 MAML 和 CertificateCloudCredentials
来管理 Redis 缓存,您将收到以下错误消息:
AuthenticationFailed:身份验证失败。缺少“授权” header 。
杠杆Fiddler ,您可以找到详细的错误如下:
考虑到您正在使用 Microsoft.Azure.Management.Redis.dll (v0.9.0)
,用于管理 Redis 缓存的代码将如下所示:
TokenCloudCredentials tokenCredential = new TokenCloudCredentials("{your-subscriptionId}", "{token}");
RedisManagementClient client = new RedisManagementClient(tokenCredential);
var redisParams = new RedisCreateOrUpdateParameters()
{
Properties = new RedisProperties(version, new Sku(redisSKUName, redisSKUFamily, redisSKUCapacity)),
Location = redisCacheRegion
};
client.Redis.CreateOrUpdate(resourceGroupName, cacheName, redisParams);
注意:
为了生成 token ,您可以重用 here 中的
GetAuthorizationHeader
函数。 .用于设置应用程序以使用 Active Directory 对 Azure 资源管理器进行身份验证
您需要set up an AD application和 assign role (Contributor) to your application通过 MAML 管理您的 Redis 缓存。更多详情可以引用这个官方tutorial .
更新
这是我的packages.config
:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.Management.Redis" version="0.9.0-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common" version="1.3.0" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common.Dependencies" version="1.1.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
</packages>
结果:
关于Azure Redis,如何使用 Microsoft Azure 管理库 (MAML) 在 .NET 中进行扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41923318/