c# - 如何传递凭据以使用 ResourceManagementClient 从 azure 资源组 C# 获取所有资源?

标签 c# azure azure-resource-manager

我已经安装了 nuget Microsoft.Azure.Management.ResourceManager 并具有以下代码来根据资源组名称获取所有现有资源

 var resouceManagementClient = new ResourceManagementClient(credentials) { SubscriptionId = "mySubscriptionId" };

            var listResources =
            resouceManagementClient.ResourceGroups.ListResources("Demo-ResourceGroup");

我不确定从哪里可以获得credentials参数值。

I do not have Azure Active Directory access , I think its must , can we bypass azure AD?.

在我的 azure 门户中,我创建了一个资源组 - Demo-ResourceGroup创建了许多资源

我只想使用 C# 代码列出所有现有资源。

最佳答案

一种方法是从 Azure AD 获取访问 token 并将其传递到 TokenCredentials 类。

var authContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantId));
var credential = new ClientCredential(applicationId, password);
AuthenticationResult token = authContext.AcquireTokenAsync("https://management.core.windows.net/", credential).Result;
var credentials = new TokenCredentials(token.AccessToken);

用于请求访问 token 的凭据集(在本例中为 clientId/secret)将确定应用程序是否具有枚举资源的适当权限。 This is a good MS docs page on how to register your application with AAD 。上例中,applicationId和password来自AAD中的应用注册

Microsoft has a page describing other ways you can get tokens from AAD .

关于c# - 如何传递凭据以使用 ResourceManagementClient 从 azure 资源组 C# 获取所有资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48241591/

相关文章:

ios - Azure DevOps 问题 : "The pipeline is not valid. 作业 mac_agent:步骤 InstallAppleProvisioningProfile 输入 provProfileSecureFile 引用安全文件

azure - Azure 部署中的deployment().name 值来自哪里?

c# - mvc5 : Cannot access a disposed object. asp.net mvc5 使用统一

c# - 当设备设置为每 30 秒锁定一次时,如何使 WP7 应用程序不进入锁定模式?

C# WPF - materialdesign 包会影响数据网格的功能吗?

azure - 在 ARM 模板中添加私有(private) nuget feed 的身份验证

Azure ARM 模板复制循环 - 对象的对象

c# - 自定义控件可绑定(bind)属性中的值未更新 - Xamarin forms/Prism

powershell - 运行安装程序-Azure 和重置-Azure Cmdlet 时出现错误

azure - azure cloudapp.net 的 SSL 证书