Magg
Magg

Reputation: 69

TokenCache: No matching token was found in the cache, Azure AD Api

I'd like to use Azure AD Api and I couldn't acquire token some reason. I have two methods, and I got this after calling:

       TokenCache: No matching token was found in the cache iisexpress.exe Information: 0 

Here's my code:

    public string GetToken()
    {
        string authority = "https://login.microsoftonline.com/{tenantId}/";
        string clientId = "";
        string secret = "";
        string resource = "https://graph.windows.net/";

        var credential = new ClientCredential(clientId, secret);
        AuthenticationContext authContext = new AuthenticationContext(authority);

        //I think the problem is here:
        var token = authContext.AcquireTokenAsync(resource, credential).Result.AccessToken;

        return token;
    }

    public string MakeRequest()
    {
        string accessToken = GetToken();
        var tenantId = "";
        string graphResourceId = "https://graph.windows.net/";

        Uri servicePointUri = new Uri(graphResourceId);
        Uri serviceRoot = new Uri(servicePointUri, tenantId);

        ActiveDirectoryClient client = new ActiveDirectoryClient(serviceRoot, async () => await Task.FromResult(accessToken));

        foreach (var user in client.Users.ExecuteAsync().Result.CurrentPage)
            Console.WriteLine(user.DisplayName);

        var client1 = new HttpClient();
        var uri = "https://graph.windows.net/" + tenantId + "/users?api-version=1.6";
        client1.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);

        var response = client1.GetAsync(uri).Result;
        var result = response.Content.ReadAsStringAsync().Result;

        return result;
    }

I don't know what's the problem, and I didn't find any great hint, under other questions and a little explanation would be helpful. I'd like to understand this part, of course.

Upvotes: 0

Views: 1846

Answers (2)

Ramanujam Allam
Ramanujam Allam

Reputation: 1386

  • //When you are calling

Main() { Method_A() }

aync Method_A() { await Method_B() }

Task < T > Method_B() { return T; }

//It will through the error. //Need to keep Mehtod_B in another Task and run.

// Here I am avoiding few asyncs

Main() { Method_A() }

Method_A() { Method_B().Wait() }

Task Method_B() { return T; }

Upvotes: 1

Fei Xue
Fei Xue

Reputation: 14649

There is no output using the Console.WriteLine in a IIS progress. If you want to output the result in a output window for the web project, you can use System.Diagnostics.Debug.WriteLine() method.

Upvotes: 0

Related Questions