【问题标题】:AcquireTokenSilent() throws Failed to acquire token silently. Call method AcquireToken errorAcquireTokenSilent() 抛出静默获取令牌失败。调用方法 AcquireToken 错误
【发布时间】:2015-07-10 22:13:57
【问题描述】:

我有一个本机客户端应用程序,我正在尝试使用 AcquireTokenSilent(),因此没有提示用户信用。但是,经过多次(多次)尝试,我只是迷失了问题所在。

我创建了自己的 PrivateTokenCacheClass,它继承自 TokenCache 类。

这是我的代码:

 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;
            }            
        }

    }

现在,在我的 AcquireToken 方法中,我这样做:

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

           AuthenticationContext authContext = new AuthenticationContext(authority, pvtCache);

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

token.txt 文件包含我之前(10 分钟前)使用此调用获得的令牌(纯文本):

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

任何帮助将不胜感激!

【问题讨论】:

    标签: c# access-token azure-active-directory adal


    【解决方案1】:

    您应该能够枚举您的缓存(ReadAll 方法)并将其内容与您传递给 AcquireTokenSilent 的参数进行比较。如果有轻微的不匹配,您将不会获得令牌。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2019-09-24
      • 1970-01-01
      相关资源
      最近更新 更多