简体   繁体   中英

how to get result of an async call?

This seem to be a pretty easy one, but as I dont have exp with async too much, then I come to the experts.

I need to make a rest call, and for it I need an authorization token.(bearer token)

This is already done by this:

private static async Task<string> GetAppTokenAsync()
{
    string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
    string appKey = ConfigurationManager.AppSettings["ida:AppKey"];
    string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
    string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
    string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
    string azureAdGraphApiEndPoint = ConfigurationManager.AppSettings["ida:AzureAdGraphApiEndPoint"];
    // This is the resource ID of the AAD Graph API.  We'll need this to request a token to call the Graph API.
    string graphResourceId = ConfigurationManager.AppSettings["ida:GraphResourceId"];

    string Authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);

    // Instantiate an AuthenticationContext for my directory (see authString above).
    AuthenticationContext authenticationContext = new AuthenticationContext(Authority, false);

    // Create a ClientCredential that will be used for authentication.
    // This is where the Client ID and Key/Secret from the Azure Management Portal is used.
    ClientCredential clientCred = new ClientCredential(clientId, appKey);

    // Acquire an access token from Azure AD to access the Azure AD Graph (the resource)
    // using the Client ID and Key/Secret as credentials.
    AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(graphResourceId, clientCred);

    // Return the access token.
    return authenticationResult.AccessToken;
}

However, from the caller method, which is a lambda expression with await, I dont know how to get a reference to that result and put in the httpheader

public ActionResult TestRestCall()
{
    Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);

    ActiveDirectoryClient adClient = new ActiveDirectoryClient(
     serviceRoot,
     async () => await GetAppTokenAsync());

    Application app = (Application)adClient.Applications.Where(
        a => a.AppId == clientId).ExecuteSingleAsync().Result;
    if (app == null)
    {
        throw new ApplicationException("Unable to get a reference to application in Azure AD.");
    }

    HttpClient hc = new HttpClient();
    hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
         "Bearer", CODE HERE);

}

You should make your caller function Async like:

public async Task<ActionResult> TestRestCall()

then call your async method:

var token = await GetAppTokenAsync();
ActiveDirectoryClient adClient = new ActiveDirectoryClient(
             serviceRoot, token);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM