【发布时间】:2017-09-12 16:34:54
【问题描述】:
四处张望,但看起来我现在被困住了。我在我的应用程序中使用 Windows Active Directory 进行身份验证。 对于授权,我正在使用声明。在搜索了有限的 .net 核心文档后,我的代码是这样的。
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IPrincipal>(
provider => provider.GetService<IHttpContextAccessor>().HttpContext.User);
services.AddTransient<IClaimsTransformation, ClaimsTransformer>();
services.AddAuthentication(IISDefaults.AuthenticationScheme);
}
ClaimsTransformer.cs
class ClaimsTransformer : IClaimsTransformation
{
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
// call to database to get more claims based on user id ClaimsIdentity.Name
((ClaimsIdentity)principal.Identity).AddClaim(new Claim("now",DateTime.Now.ToString()));
return Task.FromResult(principal);
}
}
但问题是,每次请求都会调用此代码,并且每次都从数据库加载声明,这是绝对错误的。有什么办法可以缓存吗?我能够创建一个声明 cookie 并将该 cookie 用于 .net 4.0 中的任何进一步调用。我似乎无法在核心中找到方法。我检查的任何文档都不完整或不涵盖我的情况。我可以在我的申请中进一步声明文档在这里所说的内容:https://docs.microsoft.com/en-us/aspnet/core/security/authorization/claims
但没有提及缓存声明。
有人在同一条船上吗?或者知道出路吗?
【问题讨论】:
-
我也想知道这个。我将研究@Brad 建议的内存缓存技术。虽然我不喜欢表单身份验证身份缓存在 cookie 中而 Windows 身份验证身份缓存在内存中的不一致。
-
@tanush 一个好问题。我有完全相同的问题 - 我的 TransformAsync 中的数据库调用,以及非常糟糕的官方文档。
标签: c# asp.net .net asp.net-core claims-based-identity