首页 文章

推荐的Web API ADAL令牌缓存?

提问于
浏览
3

我正在构建一个使用AAD保护的.NET核心Web API,它使用ADAL使用代表流来调用下游API ....与此Azure示例类似:

https://github.com/Azure-Samples/active-directory-dotnet-webapi-onbehalfof

在这样的场景中应该使用的令牌缓存的最佳实践是什么?

  • 默认缓存是否可以接受?

  • 你有没有缓存?

AuthenticationContext authContext = new AuthenticationContext(authority,null)

  • 如果您应该自己构建,那么有一个很好的参考实现可供使用吗?

1 回答

  • 5

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

    ADAL使用的默认缓存是内存缓存,这意味着它可能不会在API接收的请求中保留 . 此外,ADAL.NET使用的默认缓存是一个静态类,这意味着对API的两个不同请求可能会获取相同的缓存对象,这通常是意外的,因为这两个请求可能是针对不同的用户 . 因此,通常不建议使用默认的ADAL缓存 - 这实际上取决于Web服务器的工作方式 .

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

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

    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
            }
        }
    
    }
    

相关问题