Using authentication token in azure sdk fluent

前端 未结 3 1726
别那么骄傲
别那么骄傲 2021-02-05 14:27

To authenticate with Azure in azure sdk fluent nuget, there is a method that uses client id and secret as below

var azureCredentials = new AzureCredentials(new 
         


        
3条回答
  •  执笔经年
    2021-02-05 15:00

    The answer is actually yes, you can use the authentication token (JWT). It's just not that obvious.

    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);
    

    Sigh...they've changed the WithCredentials to use an AzureCredentials instead of a ServiceClientCredentials. Here's an updated version:-

    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);
    

提交回复
热议问题