0
votes

I have a native client application and I'm trying to use AcquireTokenSilent() so there isn't a prompt for user creds. However, after many (many) attempts, I'm just lost on where the problem is.

I have created my own PrivateTokenCacheClass which is inherited from TokenCache class.

Here's my code:

 public class PrivateTokenCacheClass : TokenCache
    {
        public string CacheFilePath = @"C:\Users\blahblah\Desktop\token.txt";

        public AADTokenCache()
        {
            CacheFilePath = @"C:\Users\blahblah\Desktop\token.txt";
            this.BeforeAccess = BeforeAccessNotification;
            this.AfterAccess = AfterAccessNotification;
        }

        public void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            this.Deserialize(File.ReadAllBytes(CacheFilePath));            
        }
        public void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            if(this.HasStateChanged)
            {
                File.WriteAllBytes(CacheFilePath, this.Serialize());
                this.HasStateChanged = false;
            }            
        }

    }

Now, in my AcquireToken method, I do this:

 private static string acquireAuthToken()
        {
            PrivateTokenCacheClass pvtCache = new PrivateTokenCacheClass();

           AuthenticationContext authContext = new AuthenticationContext(authority, pvtCache);

            string token = authContext.AcquireTokenSilent(resourceUri, clientID, UserIdentifier.AnyUser).AccessToken.ToString();
        }

The token.txt file contains the token (in plain text) that I got from previously (10 mins ago) using this call:

string token = authContext.AcquireToken(resourceUri, clientID, redirectUri).AccessToken.ToString();

Any help would be highly appreciated!

1

1 Answers

0
votes

You should be able to enumerate your cache (ReadAll method) and compare its content with the parameters you pass to AcquireTokenSilent. If there is a slight mismatch, you will not get a token back.