【问题标题】:Failed to acquire token silently. Call method AcquireToken not working静默获取令牌失败。调用方法 AcquireToken 不起作用
【发布时间】:2018-02-23 07:28:19
【问题描述】:
    // constructor
    public ADALTokenCache(string user)
    {
        // associate the cache to the current user of the web app
        User = user;
        this.AfterAccess = AfterAccessNotification;
        this.BeforeAccess = BeforeAccessNotification;
        this.BeforeWrite = BeforeWriteNotification;

        // look up the entry in the DB
        Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
        // place the entry in memory
        this.Deserialize((Cache == null) ? null : Cache.cacheBits);
    }

    // clean up the DB
    public override void Clear()
    {
        base.Clear();
        foreach (var cacheEntry in db.UserTokenCacheList)
            db.UserTokenCacheList.Remove(cacheEntry);
        db.SaveChanges();
    }

    // Notification raised before ADAL accesses the cache.
    // This is your chance to update the in-memory copy from the DB, if the in-memory version is stale
    void BeforeAccessNotification(TokenCacheNotificationArgs args)
    {
        if (Cache == null)
        {
            // first time access
            Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
        }
        else
        {   // retrieve last write from the DB
            var status = from e in db.UserTokenCacheList
                         where (e.webUserUniqueId == User)
                         select new
                         {
                             LastWrite = e.LastWrite
                         };
            // if the in-memory copy is older than the persistent copy
            if (status.First().LastWrite > Cache.LastWrite)
            //// read from from storage, update in-memory copy
            {
                Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
            }
        }
        this.Deserialize((Cache == null) ? null : Cache.cacheBits);
    }

    // Notification raised after ADAL accessed the cache.
    // If the HasStateChanged flag is set, ADAL changed the content of the cache
    void AfterAccessNotification(TokenCacheNotificationArgs args)
    {
        // if state changed
        if (this.HasStateChanged)
        {
            Cache = new UserTokenCache
            {
                webUserUniqueId = User,
                cacheBits = this.Serialize(),
                LastWrite = DateTime.Now
            };
            //// update the DB and the lastwrite                
            db.Entry(Cache).State = Cache.UserTokenCacheId == 0 ? EntityState.Added : EntityState.Modified;
            db.SaveChanges();
            this.HasStateChanged = true;
        }
    }

    void BeforeWriteNotification(TokenCacheNotificationArgs args)
    {
        // if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry
    }

这是我管理 AdalToken 缓存的代码 我得到了以下异常 静默获取令牌失败。调用方法 AcquireToken

var authResultDisc

附近的以下代码中出现此异常

//SharePoint 连接获取列表项

DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
 async () =>
 {
   var authResultDisc = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId, new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey), new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
             return authResultDisc.AccessToken;
                            });

                            var dcr = await discClient.DiscoverCapabilityAsync("RootSite");

我的想法是,它不会清除数据库条目。

谁能帮我解决这个错误。

【问题讨论】:

  • 异常似乎建议尝试AcquireToken - 当你使用它时会发生什么?

标签: c# azure oauth adal sharepoint-api


【解决方案1】:

如果无法在缓存中找到访问令牌并且刷新访问令牌失败,则这是一个预期的异常。调用该函数前请检查是否获取了access token。

【讨论】:

  • 您尝试检索 SharePoint 访问令牌的功能是什么?请分享有关它的代码和详细的异常。
  • 异常在我检索 SharePoint 访问令牌的同一行引发。我试过的一件事是从 app_data 文件夹中删除所有数据库并再次运行我的应用程序,它再次开始工作而没有错误,但几天后再次抛出异常,所以我担心的是从数据库中删除所有行,下一个挑战是如何删除所有数据为那个用户? AdalTokenCache 类中有 Clear() 方法,但该方法没有来自整个项目的任何调用。从我们必须调用这个函数来清除数据库中的缓存
  • 正如我在帖子中提到的,当AcquireTokenSilentAsync 函数无法获取缓存中的访问令牌并且刷新令牌失败时,预计会出现异常。刷新令牌的生命周期默认为 14 天。当您遇到此异常时,您应该再次以交互方式获取令牌。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
相关资源
最近更新 更多