2017-09-14 19 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 
    } 

작동하지 않음이 내 코드는 내가 자동으로 토큰을 획득하는 데 실패 다음과 같은 예외 을 얻었다. 통화 방법 AcquireToken토큰을 자동으로 획득하지 못했습니다. AdalToken 캐시 를 관리하기위한 호출 방법 AcquireToken이

은 목록 항목을 얻을 수 VAR authResultDisc

// 셰어 연결 근처에 코드를 다음에이 예외를 가지고

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 일이어야합니다. 이 예외가 발생하면 대화식으로 토큰을 다시 얻어야합니다. –