我试图从Azure AD系统检索身份验证令牌。我已经尝试了许多不同的配置async方法以及await命令的方法,但每次出现错误“任务被取消”。来自Web应用的Azure异步身份验证令牌
我在aspx页面里有async =“true”。
任何想法,我需要做不同的做法,以获得成功的请求和检索令牌。
相同的代码在控制台应用程序中起作用,所以我假设问题与异步操作发生的方式有关。
我的代码如下:
protected async void Login_click(object sender, EventArgs e)
{
response1.Text = "Started";
var tentantID = ConfigurationManager.AppSettings["tenantID"];
var clientId = ConfigurationManager.AppSettings["applicationID"];
var secret = ConfigurationManager.AppSettings["secret"];
await Authorize(tentantID , clientId, secret);
}
private async Task<AuthenticationResult> GetToken(string clientId, string tenantDomain, string clientSecret)
{
AuthenticationResult result = null;
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantDomain);
try
{
ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
return result = await context.AcquireTokenAsync("https://management.core.windows.net/", clientCredential).ConfigureAwait(false);
}
catch (AdalException ae)
{
//Error code
return null;
}
catch (Exception e)
{
//Error code
return null;
}
}
private async Task Authorize(string tenant, string clientId, string clientSecret)
{
var authenticationResult = await GetToken(clientId, tenant, clientSecret).ConfigureAwait(false);
string token = authenticationResult.AccessToken;
}
编辑... 我更新的代码:
protected void Login_click(object sender, EventArgs e)
{
response1.Text = "Started";
RegisterAsyncTask(new PageAsyncTask(Authorize));
}
public async Task Authorize()
{
var tentantID = ConfigurationManager.AppSettings["tenantID"];
var clientId = ConfigurationManager.AppSettings["applicationID"];
string myKey = ConfigurationManager.AppSettings["myKey"];
var tenantDomain = ConfigurationManager.AppSettings["tenantDomain"];
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantDomain, false);
try
{
ClientCredential clientCredential = new ClientCredential(clientId, myKey);
var result = context.AcquireTokenAsync("https://management.core.windows.net/", clientCredential).ConfigureAwait(false);
AuthenticationResult resVal = await result;
token = resVal.AccessToken;
}
catch (AdalException ae)
{
//error code
token = ae.InnerException.Message;
}
}
哪条线给你错误?如果你在'GetToken()'内进行调试,你是否会输入任何这些* catch *块? – Alisson
AcquireTokenAsync命令抛出AdalException的内部消息“任务被取消” –