2017-09-14 145 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天。當你得到這個異常時,你應該再次交互地獲取令牌。 –

相關問題