在azure redis缓存中使用访问令牌(连接字符串)作为身份验证方法的Auth方法名称

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

使用连接字符串(访问令牌)作为身份验证方法时,“首选数据持久性身份验证方法”应该是什么

我正在将 Redis 资源的身份验证方法从 ManagedIdentity 更改为连接字符串,应在 Redis 配置 PreferredDataPersistenceAuthMethod 中添加内容。目前它是“首选数据持久性身份验证方法”:“ManagedIdentity”

azure azure-active-directory azure-managed-identity azure-storage-account azure-redis-cache
1个回答
0
投票

以下是使用连接字符串作为身份验证方法实现与 Azure Redis 缓存资源的连接的完整代码

使用的包

StackExchange.Redis
和来自git的代码参考。


 ConnectionMultiplexer? connectionMultiplexer = null;
 StringWriter connectionLog = new();

 try
 {
    
     Console.Write("Enter Redis cache connection string: ");
     var connectionString = Console.ReadLine()?.Trim();

     
     Console.WriteLine("Connecting with a connection string...");
     connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString!, null, connectionLog);

     // Check if connection was successful
     if (connectionMultiplexer.IsConnected)
     {
         Console.WriteLine("Connected successfully!");
         Console.WriteLine();
     }
     else
     {
         Console.WriteLine("Failed to connect to Redis cache.");
         return;
     }
 }
 catch (Exception ex)
 {
     Console.WriteLine($"Failed to connect: {ex}");
     Console.WriteLine();
     return;
 }
 finally
 {
     Console.WriteLine("Connection log from StackExchange.Redis:");
     Console.WriteLine(connectionLog);
 }

  
 var database = connectionMultiplexer?.GetDatabase();
 while (true)
 {
   
     try
     {
         
         var value = database!.StringGet("key");
         database.StringSet("key", DateTime.UtcNow.ToString());
         Console.Write("+");
     }
     catch (Exception ex)
     {
       
         Console.Error.WriteLine($"Failed to execute a Redis command: {ex}");
     }

     System.Threading.Thread.Sleep(TimeSpan.FromMinutes(2));
 }

输出: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.