在Azure SDK Fluent中使用身份验证令牌

18

要在Azure SDK Fluent NuGet中进行身份验证,有一种方法可以使用客户端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 和密钥,并使用它们获取身份验证令牌,其他组件/SDK 将使用该身份验证令牌。

3个回答

23

答案实际上是肯定的,您可以使用身份验证令牌(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);

2
截至2018年4月21日,此代码无法编译 ;( - Stephane
1
这个不起作用,请参见下面hovsepm提供的答案。 - anirudhgarg

9

从Azure管理流畅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);

4
请描述一下您获取 "armAuthToken" 和 "graphAuthToken" 的方法。其他示例似乎暗示可以将Bearer令牌用于这两个输入,但我尝试后没有成功。我不确定库的底层 HttpClient 如何知道这些是 Bearer 令牌,因为这个示例并没有说明。 - Cory Gehr

0
有没有接口可以在下面的代码中创建IAzure时使用身份验证令牌(JWT)而不是使用客户端ID和密钥?
简而言之,答案是否定的。根据我的经验,如果我们想要访问相应的Azure资源,则需要从相应的资源获取身份验证令牌(JWT)。Azure有很多资源,例如AzureSQL、KeyVault、ResourceManagement等。
在我看来,使用身份验证令牌(JWT)访问所有Azure资源并没有意义。
目前,我们可以从文件、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、telemetry等。有了这个新的流畅SDK,我想使用该令牌而不必使用客户端ID和密码。是否有未来计划公开基于令牌的接口? - Saravanan
有没有未来计划公开基于令牌的接口?在我看来,使用身份验证令牌(JWT)可以访问所有Azure资源并不合理。如果您有任何想法,也可以向Azure团队提供反馈 - Tom Sun - MSFT

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接