具有 Entra ID 身份验证的 Azure Redis 缓存

问题描述 投票:0回答:1

我正在尝试将 Azure Redis 缓存添加到我的 .NET 8 api 中。我添加了连接字符串并将代码添加为:

services.AddStackExchangeRedisCache(
    options =>
    {
        options.Configuration = builder.Configuration.GetConnectionString("RedisCache");
        options.InstanceName = builder.Environment.EnvironmentName;
    })
.AddDistributedMemoryCache();

当我在连接字符串中添加密码时,这是有效的。但我不想这样做,我想使用Entra ID身份验证。所以我启用了该选项,并选择了需要使用它的网络应用程序(也在天蓝色中)。访问策略为

data owner
。但是当我省略密码时,我收到错误:

StackExchange.Redis.RedisConnectionException:'消息超时 在积压中尝试发送,因为没有连接 可用(5000ms)-上次连接异常:AuthenticationFailure 在 tsg-pas-dev-redis.redis.cache.windows.net:6380/Interactive 上, Flushed/ComputeResult,最后:ECHO,来源:SetResult,未完成:0, 上次读取:0 秒前,上次写入:0 秒前,保持活动状态:60 秒,状态: ConnectedEstablishing,mgr:10 个中的 9 个可用,最后心跳:从不, 全局:0 秒前,v:2.6.122.38350,命令=SET,超时:5000,实例: 12、qu: 1、qs: 0、aw: False、bw: SpinningDown、rs: ReadAsync、ws: 刷新,输入:0,最后输入:0,当前输入:0,同步操作:1,异步操作:1, 服务器端点:tsg-pas-dev-redis.redis.cache.windows.net:6380, conn-sec:0.23,aoc:0,mc:1/1/0,mgr:可用的 10 个中的 10 个, 客户端名称:DBSX1Y6-54977(SE.Redis-v2.6.122.38350),IOCP: (忙=1,空闲=999,最小=8,最大=1000),工人: (忙=1,空闲=32766,最小=8,最大=32767),池: (线程 = 15,队列项目 = 0,完成项目 = 347,计时器 = 3),v: 2.6.122.38350(请查看本文,了解一些可能导致超时的常见客户端问题: https://stackexchange.github.io/StackExchange.Redis/Timeouts)'

异常:错误:NOAUTH 需要身份验证。验证Redis是否 提供的密码正确。尝试的命令:ECHO

知道我缺少什么吗?我记得对于其他服务之一,我必须在主要内容中做一些事情,以获取/初始化令牌......但不知道它是否相关。

c# .net azure redis azure-redis-cache
1个回答
0
投票

根据可用文档

here
,为了使用 Entra ID 身份验证,您需要使用
Microsoft.Azure.StackExchangeRedis
Nuget 包。

此处提供了相同的代码示例:https://github.com/Azure/Microsoft.Azure.StackExchangeRedis/tree/main/sample

从此链接中,使用示例代码

DefaultAzureCredential

Write("Redis cache host name: ");
cacheHostName = ReadLine()?.Trim();
Write("'Username' from the 'Data Access Configuration' blade on the Azure Cache for Redis resource): ");
var username = ReadLine()?.Trim();

Write("Connecting using TokenCredential...");
configurationOptions = await ConfigurationOptions.Parse($"{cacheHostName}:6380").ConfigureForAzureWithTokenCredentialAsync(username!, new DefaultAzureCredential());
configurationOptions.AbortOnConnectFail = true; // Fail fast for the purposes of this sample. In production code, this should remain false to retry connections on startup
LogTokenEvents(configurationOptions);

connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(configurationOptions, connectionLog);
© www.soinside.com 2019 - 2024. All rights reserved.