【问题标题】:AcquireTokenAsync function does not return any responseAcquireTokenAsync 函数不返回任何响应
【发布时间】:2019-07-26 06:49:41
【问题描述】:

我正在尝试使用下面提到的代码在 Web 应用程序中从 AzureAD 获取所有 office365 用户的列表。但是, authContext.AcquireTokenAsync(resrouce, clientCredential) 永远不会返回控制权。我已经为控制台应用程序尝试了下面的代码,它运行良好。但是,我很想知道为什么代码不适用于 Web,或者我必须进行哪些修改才能使代码在 Web 中工作。

public static async Task<string> AcquireMyToken()
        {
            string clientId = "";
            string secrect = "";
            string resrouce = "https://graph.microsoft.com";
            string authority = "https://login.microsoftonline.com/tenanId";
            AuthenticationContext authContext = new AuthenticationContext(authority);
            ClientCredential clientCredential = new ClientCredential(clientId, secrect);
            AuthenticationResult authResult = await authContext.AcquireTokenAsync(resrouce, clientCredential);
            return authResult.AccessToken;
        } 


public static async void ListFiles(string accessToken)
        {
            var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
             (requestMessage) =>
             {
                 requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                 return Task.FromResult(0);
             }));    
            var users = await graphClient.Users.Request().GetAsync();                  
        }      

【问题讨论】:

  • 如果您提供带有超时值的CancellationToken 会发生什么?
  • @Dai,我对 Azure 还是很陌生。您是说将取消令牌传递给 AcquireTokenAync 函数吗?
  • @jay 如果您有任何疑问,请随时在此评论中提问。谢谢。

标签: azure asp.net-web-api c#-4.0 azure-active-directory office365api


【解决方案1】:

关于这个问题,你需要在Controller和html中指定你的代码。有一个示例如下。

public async Task<ActionResult> Test()
        {

            string clientId = "";
            string secrect = "";
            string resrouce = "https://graph.microsoft.com";
            string authority = "https://login.microsoftonline.com/tenanId";
            AuthenticationContext authContext = new AuthenticationContext(authority);
            ClientCredential clientCredential = new ClientCredential(clientId, secrect);
            AuthenticationResult authResult = await authContext.AcquireTokenAsync(resrouce, clientCredential);
            var token = authResult.AccessToken;
            var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
                requestMessage
                    .Headers
                    .Authorization = new AuthenticationHeaderValue("bearer", token);

                return Task.FromResult(0);
            }));
           // var events = await graphServiceClient.Me.Events.Request().GetAsync();
            var users = await graphServiceClient.Users.Request().GetAsync();

            IList<User> userlist = users.CurrentPage;

            return View(userlist);
        }

HTML:

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Sign-In with Microsoft Sample</title>
    <link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" type="text/css" />
</head>
<body style="padding:50px">
    <!--show the message your need-->
    <table class="table">
        <thead>
            <tr>
                <th scope="col">userPrincipalName</th>

                <th scope="col">Mail</th>

            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@item.UserPrincipalName</td>
                    <td>@item.Mail</td>

                </tr>
            }
        </tbody>
    </table>
</body>
</html>

更多详情请参考https://github.com/microsoftgraph/msgraph-training-aspnetmvcapp

【讨论】:

  • 我正在尝试获取用户列表,而无需完成用户登录应用程序的过程。我想在不经过任何登录过程的情况下获取用户列表。
  • 嗨。您可以使用 OAuth 2.0 客户端凭据流来获取访问令牌。它允许您使用客户端凭据获取令牌。更多详情请参考docs.microsoft.com/en-us/azure/active-directory/develop/…
猜你喜欢
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多