- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个 .NET 核心 Web API,该 API 使用 AAD 进行保护,并使用 ADAL 使用代表流调用下游 API...。类似于此 Azure 示例:
https://github.com/Azure-Samples/active-directory-dotnet-webapi-onbehalfof
应该在这样的场景中使用 token 缓存的最佳实践是什么?
最佳答案
适合您使用的 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/
如何配置 ADAL JS 以使用本地 Active Directory(Windows Server 2012 R2、ADFS)? GitHub 上的公告帖子 ( http://www.cloudid
我正在使用 Javascript(不带 Angular)向我的单页 Web 应用程序添加 AAD 身份验证。初始登录工作正常,但一个小时后, token 过期,我无法使用acquireToken 更新
我正在使用 Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory 用于对 Outlook.com API 进行身份验证的 Nuget
我正在使用在 azure ad 中进行身份验证的应用程序。我使用 adal.js 来获取访问 token 。但是访问 token 的有效期只有 1 小时。那么如何使用我在 Adal js 中的刷新 t
我有一个使用 Web 表单编写的旧应用程序。在这个项目中,我们开始将一些 Webform 转换为 SPA、Angular.js 和 WebAPI。 SPA 页面直接与 WebAPI 通信。我们的想法是
我已经使用 SPA 应用程序设置了 adal 和 adal-Angular v.1.0.10 库,并取得了巨大的成功。我正在使用 webpack,但在我的 html 页面中引用这些,希望避免全局范围问
我已经使用 SPA 应用程序设置了 adal 和 adal-Angular v.1.0.10 库,并取得了巨大的成功。我正在使用 webpack,但在我的 html 页面中引用这些,希望避免全局范围问
我正在我们的应用程序中实现 AAD 单点登录,我们将在我们的 MEAN 堆栈应用程序中使用 adal.js/adal-angular.js 库。初始化库的一部分是调用 init() 并提供租户和 cl
我在 Azure AD 中注册了一个控制台应用程序,该应用程序连接到 CRM Online(使用 these steps 配置)。它查询 Web API。 应用程序需要在没有用户交互的情况下运行...
我正在创建一个使用 Azure Active Directory 身份验证库作为身份验证提供程序的网站。我按照本教程在我的开发环境中建立了工作环境。 https://github.com/AzureA
我使用 ADAL for android 编写了下面的身份验证代码: mAuthContext = new AuthenticationContext(MainActivity.this, Const
我在 iOS 应用程序中使用 ADAL v2.5.4 自动登录时遇到问题。 当用户想要登录 MSA 帐户时,我们会使用所需的参数调用 acquireTokenWithResource,并将提示行为设置
var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Cale
我已经为 ADAL 身份验证设置了一个演示应用程序。我公司要求安装InTune应用程序:https://itunes.apple.com/us/app/intune-company-portal/id
我正在尝试开发一个VueJS单页应用程序,该应用程序会将您登录到AAD,以便我可以获取访问 token 以调用各种API(例如Graph)。 用户登录后,您必须获取 token ,并且有两种方法可
我正在使用以下代码对我的 Azure 试用帐户中的默认用户进行身份验证。 static void Main(string[] args) { GetTokenAsync
我刚开始使用 adal-node npm 包。 在示例中提到: var resource = '00000002-0000-0000-c000-000000000000'; 这个 ID 是从哪里来的?
AzureAD 支持 OAuth 资源所有者密码凭据授予。 ADAL SDK 最近添加了对其的支持 ( https://www.nuget.org/packages/Microsoft.Identit
我正在尝试从我的 Auth0 设置中获取访问 token ,我正在使用 ADAL。当我查看 fiddler 时,我不明白为什么它试图连接到以下站点: https://login.windows.net
我正在尝试将 adal.js 集成到我的应用程序中。下面是我的代码。有人可以告诉我为什么未触发身份验证吗? var app = angular.module('TestWebApp', [ 'n
我是一名优秀的程序员,十分优秀!