【问题标题】:Authenticate Azure Management SDK in .NET Core?在 .NET Core 中验证 Azure 管理 SDK?
【发布时间】:2019-10-14 18:19:54
【问题描述】:

我正在运行 ASP.NET Core 应用程序 (.Net Core 3.0) 并引用了 nuGet 包 Microsoft.Azure.Management.WebSites。似乎有六种方法可以连接到 Azure,我希望这对我的环境来说是正确的。

我正在尝试实例化 WebSiteManagementClient,以便我可以修改一些 AppService 设置(向上/向下扩展服务计划)。为此,我需要一个ServiceClientCredentials 的实例。我似乎无法找到一种方法来获得正确的凭据。

我关注了几篇不同的文章,它们都提倡不同的方法。

根据 Azure 管理 SDK 进行身份验证的最简单方法是什么?

理想情况下,避免使用 Azure Active Directory。我已尝试多次尝试设置具有适当权限的应用程序注册,但我似乎无法将其组合在一起。

连接并进行更改的应用程序将是一个在 Azure 本身中运行的 ASP.NET 网站,如果它有所作为的话。

提前致谢!

到目前为止的代码:

  using Microsoft.Azure.Management.WebSites;

  var credentials = await GetCredentials();  // <-- ???
  WebSiteManagementClient client = new WebSiteManagementClient(credentials);
  client.SubscriptionId = "xxx-xxx-xxxx-xxxx";

【问题讨论】:

    标签: azure azure-active-directory azure-sdk-.net


    【解决方案1】:

    试试这个:

    static void Main(string[] args)
    {
        string tenantId = "<your tenant ID>";
        string clientId = "<your azure ad app ID>";
        string clientSecret = "<azure ad app secret>";
        string subscriptionId = "<your subscription ID>";
    
    
        WebSiteManagementClient client = new WebSiteManagementClient(GetCredsFromServicePrincipal(tenantId, clientId, clientSecret));
        client.SubscriptionId = subscriptionId;
    
        foreach (var ap in client.app.List()) {
            Console.WriteLine(ap.Id);
        }
    
    }
    
    
    private static TokenCredentials GetCredsFromServicePrincipal(String tenantId,String clientId, String clientSecret)
    {
        var authority = @"https://login.microsoftonline.com/" + tenantId;
        var authContext = new AuthenticationContext(authority);
        var credential = new ClientCredential(clientId, clientSecret);
        var authResult = authContext.AcquireTokenAsync("https://management.azure.com", credential).GetAwaiter().GetResult();
        return new TokenCredentials(authResult.AccessToken);
    }
    

    结果(列出所有网站 ID): 由于此示例使用 ServicePrincipal 访问您的 azure 网站资源,因此您应该在订阅“访问控制 (IAM)”balde 中授予相关权限,例如为其分配“网站贡献者”和“网络计划贡献者”,使其具有权限管理您的网站资源。希望对您有所帮助。

    【讨论】:

    • 您好@user1142433,请问您的问题解决了吗?
    【解决方案2】:

    新的Azure.Identity 库似乎是在 Azure 中验证服务的推荐方式。特别是 DefaultAzureCredentials() 类可以在本地开发场景和已部署的代码中无缝运行,而无需进行任何代码更改。

    这很容易与较新的management SDKs(名称类似于Azure.ResourceManager...)一起使用,因为我们只需编写new DefaultAzureCredentials() 并在创建新客户端时将其传递给管理SDK。

    遗憾的是,旧的管理 SDK(名称类似于 Microsoft.Azure.Management... 的 SDK)无法与 Azure.Identity“开箱即用”集成。他们也不打算为这些旧 API 添加对 Azure.Identity 的支持,因为他们专注于将所有内容移植到新版本。

    但是,并非 Azure 中的每个资源都有新的版本管理 API,因此在某些情况下,您会被困在使用旧的 API 中。幸运的是,有一种相对直截了当的方法来弥补差距,并且仍然将 Azure.Identity 与那些较旧的 API 一起使用。

    有一个GitHub repo 包含如何实现此目的的示例。我认为它是由 Microsoft 团队的一位开发人员提供的,但并未得到 Microsoft 的正式支持。它没有 NuGet 包,他们建议只复制您需要的位。

    我实际上发现该示例存储库中的代码对于我的需求来说过于复杂,而在我的情况下,我所需要的就是这个。 请注意,我是从我的 F# 项目中复制的,没有对其进行测试,所以我在转换为 C# 时可能会出错,但希望它足够接近,您可以理解

    class AzureIdentityFluentCredentialAdapter : AzureCredentials
    {
        public AzureIdentityFluentCredentialAdapter(string tenantId)
            : base(default(DeviceCredentialInformation), tenantId, AzureEnvironment.AzureGlobalCloud)
        {
        }
    
        public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var creds = DefaultAzureCredential() // Use the new Azure.Identity library to get access tokens
    
            var accessToken = await creds.GetTokenAsync(
                new TokenRequestContent(new [] { "https://management.azure.com/.default" }), 
                cancellationToken);
    
            return await TokenCredentials(accessToken.Token)
                .ProcessHttpRequestAsync(request, cancellationToken);
        }
    }
    

    这个例子没有做任何令牌缓存,但出于我的目的,我并没有太在意这个。它还对我请求令牌的范围进行了硬编码,因为我知道我只会将它与 Azure 管理 API 一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 2020-06-16
      • 2017-09-05
      • 2021-07-21
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多