我正在尝试从 Azure AD 系统检索身份验证令牌。我尝试了多种不同的方式来配置异步方法和 await 命令,但每次我都收到错误“任务被取消”。
我的 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;
}
}
根据您的描述,我已通过在 .NET Framework 4.5 上创建我的 Web 窗体应用程序目标并引用Active Directory 身份验证库 3.13.8 来测试此问题。根据您的代码,它可以在我这边按预期工作并部署到 Azure Web App。这是我的git 示例,您可以参考它并尝试找出它是否可以按预期工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句