Azure 存储帐户令牌过期错误

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

我在访问天蓝色存储帐户时遇到问题。 在我的应用程序中,我使用存储帐户中的多个队列。我需要为每个队列每 30 秒调用一次 QueueClient.GetProperties() 方法。问题是我随机遇到令牌超时异常:

Azure.Identity.CredentialUnavailableException:“进程“C:\ Program Files \ Microsoft Visual Studio�2 \ Enterprise \ Common7 \ IDE \ CommonExtensions \ Microsoft \ Asal \ TokenService \ Microsoft.Asal.TokenService.exe”无法获取访问令牌30 秒内。'

要使用 VisualStudioCredential 与帐户存储建立连接 我已经尝试使用DefaultAzureCredential,但效果是一样的。

在存储帐户上我有角色(IAM):

  • 存储队列数据贡献者
  • 存储帐户贡献者

这个问题有什么解决办法或者有什么方法可以调试这个错误吗?

是否可以缓存现有令牌以避免每次间隔发送请求?

代码更新

static Dictionary<string, QueueClient> _queueStorageClient = new 
Dictionary<string, QueueClient>();

public static QueueClient QueueStorage(string? QueueName, string? accountName = null)
{

    if (!_queueStorageClient.TryGetValue(QueueName, out QueueClient? queueStorageClient))
    {
        var url="***";
        var queueStorageClient = new QueueClient(uri, new VisualStudioCredential());
    }
 return queueStorageClient;
}

定时器方法代码:

System.Timers.Timer? timer= null;

timer = new System.Timers.Timer();
timer.Interval = 30000;
timer.Elapsed += async (sender, arguments) => await Handler_OnTimer(sender, arguments, queueName);
timer.AutoReset = true;
timer.Enabled = true;

处理者:

public static async Task Handler_OnTimer(Object? source, 

System.Timers.ElapsedEventArgs e, string queueName)
    {
      ...
        QueueProperties queueProperities = await QueueStorage(queueName).GetPropertiesAsync();
...
}
azure azure-blob-storage .net-6.0 credentials azure-storage-account
1个回答
0
投票

Azure 存储帐户令牌过期错误

要解决此问题,您可以使用

DefaultAzurecredential
并在终端中使用
az login
登录。

您可以通过转到查看 -> 终端 -> 输入 az login 并使用您的 Microsoft 帐户登录来实现此目的。另外,请检查您的身份包是否处于正确的更新版本中。

关于缓存现有令牌以避免每次间隔发送请求的问题,您可以使用以下代码使用.NET通过

DefaultAzurecredential
获取队列存储的属性:

代码:

using Azure.Storage.Queues;
using Azure.Identity;
using System;
using System.Collections.Generic;
using System.Timers;


public class QueueClientManager
{
    private static Dictionary<string, QueueClient> _queueStorageClients = new Dictionary<string, QueueClient>();
    private static readonly object _lock = new object();

    public static QueueClient GetQueueClient(string queueName)
    {
        lock (_lock)
        {
            if (!_queueStorageClients.TryGetValue(queueName, out var queueClient))
            {
                var queueServiceClient = new QueueServiceClient(new Uri("https://venkat123.queue.core.windows.net"), new DefaultAzureCredential());
                queueClient = queueServiceClient.GetQueueClient(queueName);
                _queueStorageClients[queueName] = queueClient;
            }
            return queueClient;
        }
    }
}

public class Program
{
    private static async Task Handler_OnTimer(object source, ElapsedEventArgs e, string queueName)
    {
            var queueClient = QueueClientManager.GetQueueClient(queueName);
            var queueProperties = await queueClient.GetPropertiesAsync();
            Console.WriteLine($"Queue '{queueName}' Properties: Approximate message count: {queueProperties.Value.ApproximateMessagesCount}");
            Console.WriteLine($"Queue '{queueName}' Metadata:");
            foreach (var s in queueProperties.Value.Metadata)
            {
              Console.WriteLine($"{s.Key}: {s.Value}");
            }
    }

    public static void Main()
    {
        string queueName = "queue25";
        var timer = new System.Timers.Timer();
        timer.Interval = 30000; // 30 seconds
        timer.Elapsed += async (sender, arguments) => await Handler_OnTimer(sender, arguments, queueName);
        timer.AutoReset = true;
        timer.Enabled = true;
        Console.WriteLine($"Monitoring queue '{queueName}' every 30 seconds. Press Enter to exit...");
        Console.ReadLine();
    }
}

输出:

Monitoring queue 'queue25' every 30 seconds. Press Enter to exit...
Queue 'queue25' Properties: Approximate message count: 1
Queue 'queue25' Metadata:
hi: hello

enter image description here

参考:

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