我試圖從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的內部消息「任務被取消」 –