【问题标题】:Using authentication token in azure sdk fluent在 azure sdk fluent 中使用身份验证令牌
【发布时间】:2017-10-24 03:36:39
【问题描述】:

要在 azure sdk fluent nuget 中使用 Azure 进行身份验证,有一种使用客户端 ID 和密码的方法,如下所示

var azureCredentials = new AzureCredentials(new 
ServicePrincipalLoginInformation
        {
            ClientId = "ClientId",
            ClientSecret = "ClientSecret"
        }, "tenantId", AzureEnvironment.AzureGlobalCloud);

在下面的代码中创建 IAzure 时,是否有任何接口可以使用身份验证令牌 (JWT) 而不是使用客户端 ID 和机密?

_azure = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(azureCredentials)
            .WithSubscription(_subscriptionId); 

注意:我有一个单独的身份验证器模块,它自己保存客户端 ID 和机密,并使用它们来获取将由其他组件/sdks 使用的身份验证令牌。

【问题讨论】:

    标签: c# azure authentication azure-sdk-.net azure-sdk


    【解决方案1】:

    答案其实是是的,可以使用认证令牌(JWT)。只是没那么明显。

    var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false);
    var result = context.AcquireToken("https://management.core.windows.net/", clientId, new Uri("http://localhost"), PromptBehavior.Always);
    var token = result.AccessToken;
    var client = RestClient
        .Configure()
        .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
        .WithCredentials(new TokenCredentials(token))
        .Build();
    var azure = Azure
        .Authenticate(client, tenantId)
        .WithSubscription(subscriptionId);
    

    叹息...他们已将 WithCredentials 更改为使用 AzureCredentials 而不是 ServiceClientCredentials。这是一个更新的版本:-

    var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false);
    var result = context.AcquireToken("https://management.core.windows.net/", clientId, new Uri("http://localhost"), PromptBehavior.Always);
    var token = result.AccessToken;
    var tokenCredentials = new TokenCredentials(token);
    var azureCredentials = new AzureCredentials(
        tokenCredentials,
        tokenCredentials,
        tenantId,
        AzureEnvironment.AzureGlobalCloud);
    var client = RestClient
        .Configure()
        .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
        .WithCredentials(azureCredentials)
        .Build();
    var azure = Azure
        .Authenticate(client, tenantId)
        .WithSubscription(subscriptionId);
    

    【讨论】:

    • 截至 2018 年 4 月 21 日,此文件无法编译;(
    • 这不起作用,请参阅下面提供的答案 hovsepm。
    【解决方案2】:

    从 Azure Management Fluent SDK v1.10 开始,您可以使用派生自 ServiceClientCredentials 的任何凭据提供程序。换句话说,您应该能够像这样将已经获取的 Bearer 令牌字符串传递给 AzureCredentials 构造函数

    var customTokenProvider = new AzureCredentials(
                            new TokenCredentials(armAuthToken),
                            new TokenCredentials(graphAuthToken),
                            tenantId,
                            AzureEnvironment.AzureGlobalCloud);
    
    var client = RestClient
                        .Configure()
                        .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                        .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                        .WithCredentials(customTokenProvider)
                        .Build();
    
    var authenticatedClient = Azure.Authenticate(client, tenantId);
    

    【讨论】:

    • 你能描述一下你从哪里得到“armAuthToken”和“graphAuthToken”吗?其他示例似乎暗示您可以将 Bearer 令牌用于这两个输入,但我没有运气这样做。我不确定库的底层 HttpClient 如何知道这些是给定此示例的 Bearer 令牌。
    【解决方案3】:

    在下面的代码中创建 IAzure 时,是否有任何接口可以使用身份验证令牌 (JWT) 而不是使用客户端 ID 和机密?

    简而言之没有。根据我的经验,如果我们想访问相应的 Azure 资源,那么我们需要从相应的资源中获取身份验证令牌(JWT)。 Azure 有很多资源,如 AzureSQL、KeyVault、ResourceManagement 等。

    根据我的选择,使用可以访问所有 Azure 资源的身份验证令牌 (JWT) 是没有意义的

    目前,我们可以从file、ServicePrincipalLoginInformation、UserLoginInformation

    获取AzureCredentials

    如果我们想操作某个资源,那么我们可以使用身份验证令牌(JWT)来做,以 KeyVault 为例。

        public static async Task<string> GetAccessToken(string azureTenantId,string azureAppId,string azureSecretKey)
        {
    
            var context = new AuthenticationContext("https://login.windows.net/" + azureTenantId);
            ClientCredential clientCredential = new ClientCredential(azureAppId, azureSecretKey);
            var tokenResponse =await context.AcquireTokenAsync("https://vault.azure.net", clientCredential); //KeyVault resource : https://vault.azure.net
            var accessToken = tokenResponse.AccessToken;
            return accessToken;
        }
    
        var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken));
    

    【讨论】:

    • 我们正在使用服务主体和证书(不使用秘密)来获取在insightsclient、遥测等中使用的访问令牌......有了这个新的流利的sdk,我想使用那个令牌无需求助于客户 ID 和机密。未来是否有计划公开基于令牌的接口?
    • Is there any future plan to expose token based interface? 在我看来,使用可以访问所有 Azure 资源的身份验证令牌 (JWT) 是没有意义的。如果您有任何想法,也可以给您feedback azure 团队。
    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 2017-10-09
    • 2017-10-30
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    相关资源
    最近更新 更多