gpt4 book ai didi

c# - Azure错误: DefaultAzureCredential authentication failed

转载 作者:行者123 更新时间:2023-12-03 05:35:48 25 4
gpt4 key购买 nike

我正在处理官方 Azure 示例:Getting started - Managing Compute Resources using Azure .NET SDK 。并在线 resourceGroup = await resourceGroups.CreateOrUpdateAsync(resourceGroupName, resourceGroup); 出现以下错误以下代码中应用程序尝试创建 Resource Group 。我已按照注册应用程序的说明进行操作,并从此link开始由 sample 提供。并且,有assigned a role to app如下:

enter image description here

错误:

Azure.Identity.AuthenticationFailedExceptionHResult=0x80131500Message=DefaultAzureCredential authentication failed.Source=Azure.Identity

Inner Exception 2:MsalServiceException: AADSTS70002: The client does not exist or is not enabled for consumers. If you are the application developer, configure a new application through the App Registrations in the Azure Portal

static async Task Main(string[] args)
{
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var resourceClient = new ResourcesManagementClient(subscriptionId, new DefaultAzureCredential());

// Create Resource Group
Console.WriteLine("--------Start create group--------");
var resourceGroups = resourceClient.ResourceGroups;
var location = "westus2";
var resourceGroupName = "QuickStartRG";
var resourceGroup = new ResourceGroup(location);
resourceGroup = await resourceGroups.CreateOrUpdateAsync(resourceGroupName, resourceGroup);
Console.WriteLine("--------Finish create group--------");

// Create a Virtual Machine
await Program.CreateVmAsync(subscriptionId, "QuickStartRG", location, "quickstartvm");

// Delete resource group if necessary
//Console.WriteLine("--------Start delete group--------");
//await (await resourceGroups.StartDeleteAsync(resourceGroupName)).WaitForCompletionAsync();
//Console.WriteLine("--------Finish delete group--------");
//Console.ReadKey();
}

更新:

根据示例中的说明,以下是我的操作 Used the portal to create an Azure AD application and service principal that can access resources 。我可能没有在这里做某事。请让我知道我在这里没有做什么:

访问控制 (IAM) 中注册应用程序的角色分配:

enter image description here

身份验证和直接 URI:

enter image description here

注册应用程序的 API 权限:

enter image description here

更新2:

@JoyWan 合作,我能够解决这个问题(谢谢乔伊)。下面是成功创建所有必需的截图compute resources包括VM注意:单击图像可以更好地查看屏幕截图。

enter image description here

最佳答案

我测试了代码,它在我这边工作得很好。您提到的步骤也是正确的。

在此示例中,DefaultAzureCredential()实际上使用 EnvironmentCredential()在本地,因此如果您在本地运行代码,请确保您有 Set Environment Variables使用 AD 应用客户端 ID、客户端 key 、租户 ID

enter image description here

enter image description here

enter image description here

更新:

从@nam的评论来看,问题是环境变量昨天没有刷新,因为他昨天关闭了机器,今天又重新启动了它,环境变量同步了,因此应用程序开始工作。

关于c# - Azure错误: DefaultAzureCredential authentication failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62784854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com