gpt4 book ai didi

azure-active-directory - 推荐用于 Web API 的 ADAL token 缓存?

转载 作者:行者123 更新时间:2023-12-04 08:42:58 24 4
gpt4 key购买 nike

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

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

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

  • 默认缓存是否可以接受?
  • 你不应该有缓存吗?

    AuthenticationContext authContext = 新
    AuthenticationContext(authority, null)
  • 如果你应该建立自己的
    那么有没有好的引用实现可以使用呢?
  • 最佳答案

    适合您使用的 token 缓存非常主观,实际上取决于您的架构、性能要求等。

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

    相反,我们建议通过 null作为 token 缓存,如果您可以管理性能影响,或者最好实现您自己的 token 缓存。

    如果您想实现自己的缓存,它将使您的应用程序不必针对每个传入请求向 AAD(通过 ADAL)发出出站 HTTP 请求。使用 .NET Entity Framework 的示例 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
    }
    }

    }

    关于azure-active-directory - 推荐用于 Web API 的 ADAL token 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44268324/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com