1

I am trying to get a list of all office365 users from AzureAD in a web application using below mentioned code. But, authContext.AcquireTokenAsync(resrouce, clientCredential) never returns back the control. I have tried below code for console application and it worked perfectly. But, I am curious to learn why the code does not work for web or what modification do I have to make to make the code work in 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();                  
        }      
jay
  • 119
  • 3
  • 12

1 Answers1

0

Regarding the issue, you need to specify your code in the Controller and html. There is a sample as below.

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>

For more details, please refer to https://github.com/microsoftgraph/msgraph-training-aspnetmvcapp.

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • I am trying to get the list of users without having to go through the process of user signing into the application. I want to get users list without going through any sign in process. – jay Mar 05 '19 at 16:22
  • Hi. You can use OAuth 2.0 client credentials flow to get access token. It allows you to get a token with client credentials. For more details, please refer to https://learn.microsoft.com/en-us/azure/active-directory/develop/v1-oauth2-client-creds-grant-flow – Jim Xu Mar 06 '19 at 00:57