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

// 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日です。この例外が発生した場合、トークンを対話的に再度取得する必要があります。 –

関連する問題