Azure active directory 推荐用于Web API的ADAL令牌缓存?

Azure active directory 推荐用于Web API的ADAL令牌缓存?,azure-active-directory,adal,Azure Active Directory,Adal,我正在构建一个.NET核心Web API,该API使用AAD进行保护,并使用ADAL使用代表流调用下游API…。与此Azure示例类似: 在这样的场景中,令牌缓存的最佳实践是什么 默认缓存是否可接受 你是否应该没有缓存 AuthenticationContext authContext=新建 AuthenticationContext(授权,空) 如果你应该建立自己的 那么,是否有一个好的参考实现可供使用 您要使用的正确令牌缓存是非常主观的,实际上取决于您的体系结构、性能要求等 ADAL使用

我正在构建一个.NET核心Web API,该API使用AAD进行保护,并使用ADAL使用代表流调用下游API…。与此Azure示例类似:

在这样的场景中,令牌缓存的最佳实践是什么

  • 默认缓存是否可接受

  • 你是否应该没有缓存

    AuthenticationContext authContext=新建 AuthenticationContext(授权,空)

  • 如果你应该建立自己的 那么,是否有一个好的参考实现可供使用


您要使用的正确令牌缓存是非常主观的,实际上取决于您的体系结构、性能要求等

ADAL使用的默认缓存是内存缓存,这意味着它可能不会在API接收的请求之间持久化。此外,ADAL.NET使用的默认缓存是静态类,这意味着对API的两个不同请求可能拾取相同的缓存对象,这通常是意外的,因为这两个请求可能针对不同的用户。因此,通常不建议使用默认的ADAL缓存—它实际上取决于web服务器的工作方式

相反,如果您可以管理性能命中,我们建议将
null
作为令牌缓存传递,或者最好实现您自己的令牌缓存

如果您想实现自己的缓存,它将使您的应用程序不必在每次传入请求时向AAD发出出站HTTP请求(通过ADAL)。使用.NET实体框架的ADAL缓存实现示例可用,并复制如下:

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace TodoListWebApp.DAL
{

    public class PerWebUserCache
    {
        [Key]
        public int EntryId { get; set; }
        public string webUserUniqueId { get; set; }
        public byte[] cacheBits { get; set; }
        public DateTime LastWrite { get; set; }
    }

    public class EFADALTokenCache: TokenCache
    {
        private TodoListWebAppContext db = new TodoListWebAppContext();
        string User;
        PerWebUserCache Cache;

        // constructor
        public EFADALTokenCache(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.PerUserCacheList.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.PerUserCacheList)
                db.PerUserCacheList.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.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
            }
            else
            {   // retrieve last write from the DB
                var status = from e in db.PerUserCacheList
                             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.PerUserCacheList.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 PerWebUserCache
                {
                    webUserUniqueId = User,
                    cacheBits = this.Serialize(),
                    LastWrite = DateTime.Now
                };
                //// update the DB and the lastwrite                
                db.Entry(Cache).State = Cache.EntryId == 0 ? EntityState.Added : EntityState.Modified;                
                db.SaveChanges();
                this.HasStateChanged = false;
            }
        }
        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
        }
    }

}

刚刚无意中发现了这一点,是否有必要在Clear()方法中标记Cache=null,否则永远不会从变量中删除该值,然后在BeforeAccess方法中,在不应该的情况下传递第一个if。因此,您也可以为错误的凭据获取令牌。EFADALTOKENCHACHE真的是一个很好的例子吗?(是否有人真的将其用于制作应用程序?)。它似乎没有按预期的那样工作。Clear方法为所有用户而不是特定用户清除。此外,作为“更新”的代码实际上插入了新值。