【问题标题】:Recommended ADAL token cache for Web API?推荐用于 Web API 的 ADAL 令牌缓存?
【发布时间】:2017-10-31 06:54:20
【问题描述】:

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

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

在这种情况下应该使用令牌缓存的最佳实践是什么?

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

  • 你应该没有缓存吗?

    AuthenticationContext authContext = new AuthenticationContext(authority, null)

  • 如果您应该构建自己的 那么有没有好的参考实现可以使用?

【问题讨论】:

    标签: azure-active-directory adal


    【解决方案1】:

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

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

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

    如果您想实现自己的缓存,它将使您的应用不必在每次传入请求时都向 AAD(通过 ADAL)发出出站 HTTP 请求。 here 提供了一个使用 .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 在应该传递的时候传递不是。因此,您也可以获得错误凭据的令牌。
    • EFADALTokenCache 真的是一个很好的例子吗? (有人真的将它用于生产应用程序吗?)。它似乎没有按预期工作。 Clear 方法会清除所有用户而不是特定用户。此外,旨在作为“更新”的代码实际上会插入新值。
    猜你喜欢
    • 1970-01-01
    • 2017-07-01
    • 2018-07-12
    • 2017-06-07
    • 2018-04-14
    • 2014-06-04
    • 2018-01-13
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多