无法以静默方式获取令牌。调用方法AcquireToken无效

时间:2017-09-14 10:43:27

标签: c# azure oauth adal sharepoint-api

    // 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 Cache的代码 我得到以下异常 无法以静默方式获取令牌。调用方法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");

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

任何人都可以帮我解决这个错误。

1 个答案:

答案 0 :(得分:0)

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

相关问题