locked
C# Azure AD - Getting List of Users and current user's title - Stuck in loop RRS feed

  • Question

  • User-1662920186 posted

    So I am trying to basically check Azure AD for the current user's title and provide a drop down list of all the users in Azure AD but I keep getting a timeout. 

    Any suggestions would be helpful. Here is what I have so far:

    private async Task<bool> getInfo()
    {
    string currentUser = HttpContext.Current.User.Identity.Name;
    string tenantId = "tId";
    string applicationID = "appId";
    string applicationKey = "appKey";

    AzureGraphAuthenticationProvider graphAuth = new AzureGraphAuthenticationProvider(); //This class is defined in bradley.cs and gets the token
    graphAuth.tenantId = tenantId;
    graphAuth.applicationID = applicationID;
    graphAuth.applicationKey = applicationKey;
    GraphServiceClient graphClient = new GraphServiceClient(graphAuth); //sets graph client using previously obtained token


    User user = await graphClient.Users[currentUser].Request().GetAsync();

    return true;
    }

    And I went into my azure portal, clicked on my "Azure Active Directory", then "App Registrations", registered a new application, grabbed the applications ID, and created a key and saved it. The app has access to Azure AD but it still just spins and eventually times out.

    Wednesday, August 1, 2018 5:44 PM

Answers

  • User-1662920186 posted

    I am just doing a "bool x = getInfo().Result", is that incorrect? When I use that is just spins until it times out. Still getting used to switching to async.

    So when I use it as a void it works and I get a value from AAD but when I run it as a task<bool> it locks up.

    Here is the solution for anyone that is curious:

    Task.Run(async () => { return await getInfo(); }).Result;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 2, 2018 12:13 PM

All replies

  • User753101303 posted

    Hi,

    A timeout would me make think first about a firewall or proxy issue. It happens on GraphClient or could it be inside AzureGraphAuthenticationProvider instead ?

    I would perhaps try a search based on the error message as well as the method in which it happens (see the call stack). It could help finding a post from someone who had the same issue.

    Wednesday, August 1, 2018 10:43 PM
  • User1724605321 posted

    Hi MethodDev,

    Please use Fiddler to trace the requests  check the detailed error message . In addition ,since you are using Microsoft Graph .NET Client Library , please refer to below link for code samples :

    https://github.com/microsoftgraph/msgraph-sdk-dotnet

    Best Regards,

    Nan Yu

    Thursday, August 2, 2018 2:40 AM
  • User-1662920186 posted

    PatriceSc

    Hi,

    A timeout would me make think first about a firewall or proxy issue. It happens on GraphClient or could it be inside AzureGraphAuthenticationProvider instead ?

    I would perhaps try a search based on the error message as well as the method in which it happens (see the call stack). It could help finding a post from someone who had the same issue.

    I am looking into that currently to see if that is what is causing the issue.

    This is the "AzureAgraphAuthenticationProvider":

    public class AzureGraphAuthenticationProvider : IAuthenticationProvider
    {
    public string tenantId { get; set; }
    public string applicationID { get; set; }
    public string applicationKey { get; set; }

    public async Task AuthenticateRequestAsync(HttpRequestMessage request)
    {

    HttpClient client = new HttpClient();

    ClientCredential credential = new ClientCredential(applicationID, applicationKey);

    var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
    var authenticationResult = authenticationContext.AcquireToken("https://graph.microsoft.com/", credential);

    string token = authenticationResult.AccessToken;

    request.Headers.Add("Authorization", "Bearer " + authenticationResult.AccessToken);
    }
    }

    Thursday, August 2, 2018 10:25 AM
  • User-1662920186 posted

    Hi MethodDev,

    Please use Fiddler to trace the requests  check the detailed error message . In addition ,since you are using Microsoft Graph .NET Client Library , please refer to below link for code samples :

    https://github.com/microsoftgraph/msgraph-sdk-dotnet

    Best Regards,

    Nan Yu

    I've not used fiddler before so I am unsure of what I am looking at.

    Thursday, August 2, 2018 10:51 AM
  • User-1662920186 posted

    Update:

    When I switch my async to async void instead of Task<bool> it runs but I get an insufficient privileges message. So maybe you can't have these ran as async Task<bool>?

    Thursday, August 2, 2018 11:39 AM
  • User753101303 posted

    authenticationResult could just return the type name rather than the expected token. I checked some code and if you have this method try :

    request.Headers.Add("Authorization", authenticationResult.CreateAuthorizationHeader());

    Else if you suspect a value is wrong, look at the value you are using to see if this is what you expect.

    Not sure what you mean by a "compact error" ? IMO it's easier to just post the error message so that others don't have to guess which error you have.

    Thursday, August 2, 2018 11:58 AM
  • User-1662920186 posted

    I think its overall permissions, switched from "public async task<bool>" to "public async void" and was able to get it to load but with an insufficient privileges error which I should be able to correct today. Now I am curious why it gets stuck when I set it to "public async task<bool>", odd.

    Thursday, August 2, 2018 12:05 PM
  • User753101303 posted

    How do you call getInfo when it returns a Task<bool> ?

    Thursday, August 2, 2018 12:10 PM
  • User-1662920186 posted

    I am just doing a "bool x = getInfo().Result", is that incorrect? When I use that is just spins until it times out. Still getting used to switching to async.

    So when I use it as a void it works and I get a value from AAD but when I run it as a task<bool> it locks up.

    Here is the solution for anyone that is curious:

    Task.Run(async () => { return await getInfo(); }).Result;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 2, 2018 12:13 PM