【问题标题】:Azure Async Authentication Token from Web App来自 Web 应用的 Azure 异步身份验证令牌
【发布时间】:2017-03-16 12:04:36
【问题描述】:

我正在尝试从 Azure AD 系统检索身份验证令牌。我尝试了多种不同的方式来配置异步方法以及 await 命令,但每次我收到错误“A Task was cancelled”。

我的 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 块?
  • AcquireTokenAsync 命令抛出 AdalException,内部消息为“A Task was cancelled”

标签: c# asp.net azure asynchronous adal


【解决方案1】:

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

【讨论】:

  • 谢谢。但是,一旦我部署到 Azure Web 应用程序,它就开始工作了,本地的相同代码不会给我令牌。
【解决方案2】:

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

我相信您的Login_Click 方法应该使用这一行来异步调用Authorize

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

然后Login_Click 应该只返回不带async 关键字的void

【讨论】:

  • 可悲的是,我没有更进一步了。我已经按照建议更改了我的代码,但是,这并没有让我更进一步。我仍然收到与之前提到“任务已取消”相同的消息 已编辑问题中的更新代码。
猜你喜欢
  • 1970-01-01
  • 2021-01-26
  • 2019-07-02
  • 2020-01-12
  • 1970-01-01
  • 2017-07-20
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多