Call to ApplicationTokenProvider.LoginSilentAsync

2019-07-15 00:03发布

问题:

From my local PC I'm trying to connect to the AzureBillingAPI using the following code :

var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantDomain, clientId, secret);

This methods never returns. Debug output displays the following :


Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 : 07/03/2017 14:02:07: e608ec23-675f-4828-aa65-72479409ec63 - TokenCache: Looking up cache for a token...

iisexpress.exe Information: 0 : 07/03/2017 14:02:07: e608ec23-675f-4828-aa65-72479409ec63 - TokenCache: Looking up cache for a token...

Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 : 07/03/2017 14:02:07: e608ec23-675f-4828-aa65-72479409ec63 - TokenCache: No matching token was found in the cache

iisexpress.exe Information: 0 : 07/03/2017 14:02:07: e608ec23-675f-4828-aa65-72479409ec63 - TokenCache: No matching token was found in the cache



Question: how do I connect ? Thanks.

回答1:

According to your description, I guess the reason why this methods never returns is you called the async method in the sync Controller method with wait keywords.

If you call the getResultAsync as below, it will never return the result :

    public ActionResult Index()
    {
       getResultAsync().Wait();
        return View();
    }

    public static async Task getResultAsync()
    {
        string subscription = "subid";

        string tenantid = "tenantid ";
        string clientId = "clientId ";
        string key = "key";

        var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantDomain, clientId, key);

        BillingManagementClient b1 = new BillingManagementClient(serviceCreds) { SubscriptionId = subscription };


        var result = b1.Operations.List();

    }

I suggest you could change the index method as below, it will work well:

   public async Task<ActionResult> Index()
        {
            await getResultAsync();
            return View();
        }

Result:



标签: c# rest azure