[英]Admin claim added to IdentityUser, but the usermanager does not find it with IsInRoleAsync
[英]Does UserManager<IdentityUser> have cache?
我有一个使用Microsoft.AspNetCore.Identity.UserManager来管理用户的应用程序。 我使用核心2.2。 UserManager是通过服务中的内置依赖项注入(不是由我)注入的 。 像这样:
public MyService(UserManager<IdentityUser> userManager)
{
_userManager = userManager;
}
当我第一次尝试通过ID(FindByIdAsync)获取用户时,我在控制台中看到对db的请求。 具有相同ID的下一个请求我看不到对db的任何请求,但是该应用程序正常工作并获得了用户。
在Startup.cs中,我没有userManager的配置。 我只是使用扩展方法:
public void ConfigureServices(IServiceCollection services)
{
services
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("MyConnectionString"));
services
.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
services.AddMvc();
var configService = new ConfigurationService(Configuration);
services
.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryApiResources(configService.GetApiResources())
.AddInMemoryClients(configService.GetClients())
.AddTestUsers(configService.GetUsers());
}
问题 :也许有一种方法可以在UserManager中配置此缓存功能? 指出我在哪里可以获得有关此缓存剩余时间的信息。
我在Github上浏览了Identity源代码,并以UserStore
:
public override Task<TUser> FindByIdAsync(string userId, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
var id = ConvertIdFromString(userId);
return UsersSet.FindAsync(new object[] { id }, cancellationToken);
}
类型为DbSet<TUser>
UsersSet
beeing,Identity无法直接管理缓存,但依赖于EF内部。
深入研究EF源代码,我以DbContext
:
object IDbSetCache.GetOrAddSet(IDbSetSource source, Type type)
{
CheckDisposed();
if (_sets == null)
{
_sets = new Dictionary<Type, object>();
}
if (!_sets.TryGetValue(type, out var set))
{
set = source.Create(this, type);
_sets[type] = set;
}
return set;
}
看起来有一个简单的Dictionary
支持的缓存机制。
但是,正如Matt G.在其评论中指出的那样,管理缓存的解决方案位于服务之上:它与注入UserManager
时如何管理依赖范围有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.