簡體   English   中英

Azure 服務總線 - 未經授權的訪問。 執行此操作需要“發送”聲明

[英]Azure Service Bus - Unauthorized access. 'Send' claim(s) are required to perform this operation

我正在嘗試從我的Windows Service應用程序訪問Azure Service Bus Queue 我正在關注這個示例。

我想使用Azure Service Principal保護這個Azure Service Bus以下是我已經實施的步驟

  1. Azure Ad中注冊一個名為pc-shutdown-producer的應用程序代表我的Windows Service
  2. 我創建了名為shutdowncomputer的 Azure service bus namespace
  3. Access control (IAM)內部,我添加了具有以下值的Role Assignment
    • 角色 - Azure Service Bus Data Owner
    • 分配訪問權限 - pc-shutdown-producer

據我所知,上述配置將使pc-shutdown-producer應用程序管理 servicebus 命名空間中的所有資源。 4.除此之外,我還提供了pc-shutdown-producer delegated API 訪問服務總線命名空間的權限。

在此處輸入圖像描述

下面是我的 C# 代碼。

        public async Task Init()
        {
            string authority = $"https://login.windows.net/{TenantId}";

            ITokenProvider tokenProvider = TokenProvider.CreateAzureActiveDirectoryTokenProvider(AuthenticationCallback, authority);
            var endpoint = new Uri($"sb://shutdowncomputer.servicebus.windows.net/");
            var entityPath = "shutdownrequest";

            var qc = new QueueClient(endpoint.ToString(), entityPath, tokenProvider);

            Message m = new Message();
            m.Body = Encoding.ASCII.GetBytes("{id: 1, name: 'hemant'}");
            m.ContentType = "application/json";
            try
            {
                await qc.SendAsync(m);
            }
            catch (Exception ex)
            {
                //I am getting exception here. 
                //Unauthorized access. 'Send' claim(s) are required to perform this operation.
                throw ex;
            }
        }

        private async Task<string> AuthenticationCallback(string audience, string authority, object state)
        {
            string accessToken = string.Empty;
            IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(AppId)
                .WithAuthority(authority)
                .WithClientSecret(Password)
                .Build();

            var serviceBusAudience = new Uri("https://servicebus.azure.net");

            List<string> claims = new List<string>();
            claims.Add($"{serviceBusAudience}/.default");
            try
            {
                var result = await app.AcquireTokenForClient(claims.ToArray()).ExecuteAsync();
                accessToken = result.AccessToken;
            }
            catch (Exception ex)
            {
                //No issue here.
                Console.WriteLine(ex.Message);
            }
            //Successfully able to retrieve a token.
            return accessToken ;
        }

執行Init()后,我收到以下異常消息。

Unauthorized access. 'Send' claim(s) are required to perform this operation. Resource: 'sb://shutdowncomputer.servicebus.windows.net/shutdownrequest'. TrackingId:52c0eedcf19d4513a8ec105943859764_G12, SystemTracker:gateway7, Timestamp:2020-05-11T06:59:01

更新 1

根據@Carl Zhao 的建議,我已經向pc-shutdown-producer提供了管理員許可,但仍然有同樣的問題。

在此處輸入圖像描述

謝謝

權限不足,應添加管理員同意: 在此處輸入圖像描述

就我而言,該錯誤具有誤導性

原因是配置文件中缺少密碼(這發生在 Jenkins 測試代理中,它正在從其他未設置密碼的文件中檢索密碼)。

如果我的密碼錯誤,則錯誤只是“嘗試執行未經授權的操作。”。 但是沒有任何密碼(例如空屬性值),錯誤是“未經授權的訪問。執行此操作需要'發送'聲明。”。

確保為應用程序分配Azure 服務總線數據所有者角色而不是所有者(根據OP 的評論)。

Azure 門戶中“添加角色分配”頁面的屏幕截圖,圈出兩個名稱相似的角色:“所有者”和“Azure 服務總線數據所有者”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM