【问题标题】:AcquireTokenAsync only working in console appAcquireTokenAsync 仅在控制台应用程序中工作
【发布时间】:2017-12-19 17:48:43
【问题描述】:

我创建了一个获取 Azure 令牌的控制台应用程序,我可以与 Graph API 通信没有问题。当我在 Windows 或 Web 应用程序中使用相同的逻辑时,AcquireTokenAsync 无法获取令牌。不会捕获任何异常,也不会成功发出令牌。下面是我在控制台应用程序和 Windows 应用程序中使用的代码,但它仅适用于控制台应用程序。我确保所有依赖项都是相同的版本。

        private static async Task<string> GetAccessToken(string password)
    {
        var authenticationContext = new AuthenticationContext(Authority, false);
        var cac = new ClientCredential(ClientId, password);
        var authenticationResult = await authenticationContext.AcquireTokenAsync(GraphUrl, cac); //never issues on Windows app
        return authenticationResult.AccessToken;
    }

有什么想法吗?

【问题讨论】:

  • 你是怎么调用这个方法的,调用那个方法的方法等等?链中是否有任何async void 方法?你不是在等待你应该等待的东西吗?
  • 我这样称呼它: var token = GetAccessToken(password);就像我在控制台应用程序中一样。链中没有异步空隙。等待 AcquireToken
  • var tokenTask,如果您不等待调用或返回的Task,则不是字符串。
  • 谢谢!我将其更改为 Task authenticationResult = authenticationContext.AcquireTokenAsync(GraphUrl, cac); authenticationResult.Wait();返回 authenticationResult.Result.AccessToken;
  • 找到答案的可以自己添加答案。

标签: c# azure token


【解决方案1】:

好吧,我在 cmets 中被指出了正确的路径,我真的不明白为什么,但以下代码解决了这个问题:

Task<AuthenticationResult> authenticationResult = authenticationContext.AcquireTokenAsync(GraphUrl, cac);
            authenticationResult.Wait();
            return authenticationResult.Result.AccessToken;

【讨论】:

  • 你的 graphUrl 是什么?
猜你喜欢
  • 2011-07-28
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多