I am trying to get an access token from Azure AD, and the async call never finishes. I am not sure what I am doing wrong, maybe someone can point me in the right direction. This is the code:
private static void GetAccessTokenNonAsync()
{
Func<System.Threading.Tasks.Task> task = async () => { await GetAccessToken().ConfigureAwait(false); };
task().Wait();
}
private static async Task<AuthenticationResult> GetAccessToken()
{
string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
string clientSecret = ConfigurationManager.AppSettings["ida:ClientSecret"];
string source = ConfigurationManager.AppSettings["ExchangeOnlineId"];
var authContext = new AuthenticationContext(aadInstance, false);
var credentials = new ClientCredential(clientId, clientSecret);
var appRedirectUrl = HttpContext.Current.GetOwinContext().Request.Scheme + "://" + HttpContext.Current.GetOwinContext().Request.Host + HttpContext.Current.GetOwinContext().Request.PathBase + "/";
var res =
await
authContext.AcquireTokenByAuthorizationCodeAsync(
((ClaimsIdentity)HttpContext.Current.User.Identity).FindFirst("AuthenticationCode").Value,
new Uri(appRedirectUrl), credentials, source).ConfigureAwait(false);
Current.AccessToken = res.AccessToken;
return res;
}
private static string AccessToken
{
get
{
return HttpContext.Current.Session["AccessToken"].ToString();
}
set { HttpContext.Current.Session["AccessToken"] = value; }
}
And in my main method I call GetAccessTokenNonAsync()