2017-09-14 135 views
-1
// 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无法默认获取令牌。调用方法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"); 

我对这个想法是,它的剂量不清除数据库条目。

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

+0

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

回答

0

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

+0

您试图为SharePoint检索访问令牌的功能是什么?请分享关于它的代码和详细的例外。 –

+0

异常将在我检索SharePoint的访问令牌的同一行中引发。 一件事我想,删除了App_Data文件夹 所有数据库并再次运行我的应用程序,它的启动无错误 再次工作,但例外过了些日子 所以我关心的是删除数据库中的所有行再次抛出,下一个挑战是如何删除该用户的所有数据? AdalTokenCache类中存在Clear()方法,但该方法没有从整个项目进行任何调用。我们必须调用此函数来清除数据库中的缓存 –

+0

正如我在文章中提到的那样,当AcquireTokenSilentAsync函数无法获取缓存中的访问令牌并刷新令牌失败时,预计会出现异常。刷新令牌的生命周期默认为14天。当你得到这个异常时,你应该再次交互地获取令牌。 –

相关问题