簡體   English   中英

Azure Function 應用程序 w/0Auth 錯誤 - 錯誤:AADSTS500011:資源主體名為

[英]Azure Function App w/ 0Auth error - ERROR: AADSTS500011: The resource principal named

我需要將 0Auth 添加到我的 Azure Function 應用程序中,這將通過 HTTP 請求、POST 來觸發,並且我應該檢索我將憑據傳遞到正文中的用戶詳細信息。 我是編碼的新手,到目前為止我看過的所有教程,由於我的經驗不足,對我來說這只是一堆毫無意義的代碼,其中包含許多細節,在我看來只有有經驗的人才能做到跟隨。 我能夠生成我的令牌,但不能在我的請求中一起傳遞它,當我調用 API 時,它會拋出以下錯誤:

Parameters: Connection String: [No connection string specified], Resource: http://localhost:7235/api/.default, Authority: . Exception Message: Tried to get token using Azure CLI. Access token could not be acquired. ERROR: AADSTS500011: The resource principal named http://localhost:7235/api/.default was not found in the tenant named <CN>. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.

作為令牌提供者,我使用的是 class AzureServiceTokenProvider(); ,我認為這是導致此錯誤的原因。 我為 Token Providers 尋找了一些替代方案,但我只能找到TokenProver(); 但我嘗試使用其中的一些,但我也遇到了錯誤。 我被要求使用 C# 做所有事情,而不是使用 Portal Azure。 我可以從 Azure 使用的唯一東西是 Azure 存儲帳戶。 我能夠創建“CRUD”系統,它們工作正常。 我沒有更多與令牌相關的 class 了。

這是我下面的代碼:

public static async Task<IActionResult> Login(
            [HttpTrigger(AuthorizationLevel.Admin, "POST", Route = "login")] HttpRequest req,
            [Table("User", Connection = "AzureWebJobsStorage")] TableClient tdClient,
            ILogger log)
        {
            string url = "http://localhost:7235/api/.default";
            var httpClient = new HttpClient();
            var azureServiceTokenProvider = new AzureServiceTokenProvider();
            string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(url);

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var input = JsonConvert.DeserializeObject<CreateUserDto>(requestBody);
            var user = new User
            {
                email = input.email,
                password = input.password
            };

            string userToString = user.ToString();
            var stringContent = new StringContent(userToString, Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(url, stringContent).ConfigureAwait(false);

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            return new OkObjectResult(response);
        }

我真的不想使用AzureServiceTokenProvider(); class。 是否可以使用TokenProvider(); . 我怎樣才能做到這一點? 我希望我說清楚了。 謝謝。

如果要強制執行身份驗證,則應考慮 Azure 功能/應用服務身份驗證或

https://docs.microsoft.com/en-us/azure/azure-functions/security-concepts?tabs=v4#enable-app-service-authenticationauthorization

暫無
暫無

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

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