2017-03-16 64 views
0

我试图从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; 
    } 
} 
+0

哪条线给你错误?如果你在'GetToken()'内进行调试,你是否会输入任何这些* catch *块? – Alisson

+0

AcquireTokenAsync命令抛出AdalException的内部消息“任务被取消” –

回答

0

根据您的描述,我已通过在.NET Framework 4.5上创建我的Web窗体应用程序目标并引用Active Directory Authentication Library 3.13.8来测试此问题。根据您的代码,它可以像我预期的那样工作并部署到Azure Web App。这是我的git sample,你可以参考它并试图找出它是否可以按预期工作。

+0

谢谢你。一旦我部署到Azure Web应用程序开始工作,但是,本地相同的代码不会给我的令牌。 –

0

async void方法ASP.NET是不是一件好事。有关它的一些信息,请参阅https://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx

我相信你Login_Click方法应使用此行来调用Authorize异步:

RegisterAsyncTask(new PageAsyncTask(Authorize(tentantID, clientId, secret))); 

然后Login_Click应该原封不动地返回void没有async关键字。

+0

可悲的是我没有更进一步。我已经改变了我的代码,但是,这并没有让我更进一步。我仍然收到与之前提到的“任务已取消”相同的消息。已编辑问题中的更新代码。 –

相关问题