AcquireTokenAsync仅在控制台应用程序中工作

问题描述 投票:0回答:1

我创建了一个获取Azure令牌的控制台应用程序,我可以与Graph API通信没问题。当我在Windows或Web应用程序中使用相同的逻辑时,AcquireTokenAsync无法获得令牌。没有捕获异常,并且永远不会成功发出令牌。下面是我在Console应用程序和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;
    }

有任何想法吗?

c# azure token
1个回答
1
投票

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

Task<AuthenticationResult> authenticationResult = authenticationContext.AcquireTokenAsync(GraphUrl, cac);
            authenticationResult.Wait();
            return authenticationResult.Result.AccessToken;
© www.soinside.com 2019 - 2024. All rights reserved.