ASP.NET Identity的UserManager可以缓存用户吗? [英] ASP.NET Identity's UserManager caches users?

查看:72
本文介绍了ASP.NET Identity的UserManager可以缓存用户吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在将ASP.NET Identity 2.2与ASP.NET MVC 5.2,实体框架6.2和Unity 5.7一起使用.

We're using ASP.NET Identity 2.2 with ASP.NET MVC 5.2, Entity Framework 6.2 and Unity 5.7.

我们有一个类ConnectUserManager,该类派生自ASP.NET Identity的UserManager.每次都会将新构造的UserStore传递给UserManager.

We have a class, ConnectUserManager, that derives from ASP.NET Identity's UserManager. A newly constructed UserStore is passed to UserManager every time.

ConnectUserManager(因此也为UserManager)的生存期是根据请求进行的:

The lifetime of ConnectUserManager (and thus of UserManager) is per request:

Container.RegisterType<ConnectUserManager>(
    new PerRequestLifetimeManager(),
    new InjectionConstructor(
        Container.Resolve<ConnectDbContext>(),
        Container.Resolve<ITemplateManager>(),
        Settings.MaxFailedAccessAttemptsBeforeLockout,
        Settings.AccountLockoutTimeSpan));

当我们需要显示给定用户的详细信息时,控制器操作将按如下方式检索用户:

When we need to display the details for a given user, a controller action retrieves the user as such:

public async Task<ActionResult> Details(int id)
{
    var user = await UserManager.FindByIdAsync(id);
    ...
}

其中UserManager是注入的属性:

[Dependency]
public ConnectUserManager UserManager { get; set; }

问题在于user似乎来自缓存:数据库中的修改似乎对我们的应用程序显示的内容没有任何影响.

The problem is that user appears to come from a cache: modifications in the database don't appear to have any effect on what our application displays.

此代码已投入生产一年,我们从未遇到任何问题:当我们的代码修改用户时,缓存似乎已正确失效.

This code has been in production for a year and we never had any issue with it: the cache seems properly invalidated when our code modifies a user.

我们现在只注意到了这个问题,因为当Identity将用户锁定时,它会更新用户的LockoutEndDateUtc属性,但似乎不会使缓存无效,并且在显示中将得到一个过时的LockoutEndDateUtc值.

We only noticed the problem now because when Identity locks a user out, it updates the user's LockoutEndDateUtc property but seemingly without invalidating the cache, and we get a stale LockoutEndDateUtc value in our display.

我们在做什么错了?

编辑:

@DotNetMatt在评论中链接了以下问题:
aspnet身份中的实体框架缓存.

@DotNetMatt linked the following question in a comment:
Entity Framework caching in aspnet Identity.

除非我缺少任何东西,否则(无论是在撰写本文时)被接受的解决方案似乎与原始海报的作者和我的问题完全无关.

Unless I'm missing something, the accepted solution (at the time of writing anyway) seems completely unrelated to the original poster's and to my problem.

但是,原始张贴者似乎自己找到了(a?)解决方案:我所做的是在我自己的用户存储区中实现的,并手动访问EF并使用.AsNoTracking()来避免缓存."

However, the original poster seems to have found the (a?) solution by himself: "What I did was implemented my own userstore and manually accessed EF and used .AsNoTracking() to avoid the caching."

有什么方法可以执行此操作而不必重新实现(或子类化)用户存储吗?

Is there any way to do this without having to reimplement (or subclass) the user store?

推荐答案

我发现了我的问题. @trailmax是正确的(在评论他自己的答案时),我周围有圈养依赖.

I found my problem. @trailmax was correct (in comments to his own answer) that I had captive dependencies lying around.

该错误实际上是我的问题的代码片段,该问题配置了ConnectUserManager依赖项的注入:

The bug was actually in the snippet of code from my question that configures injection of ConnectUserManager dependencies:

Container.RegisterType<ConnectUserManager>(
    new PerRequestLifetimeManager(),
    new InjectionConstructor(
        Container.Resolve<ConnectDbContext>(),
        Container.Resolve<ITemplateManager>(),
        Settings.MaxFailedAccessAttemptsBeforeLockout,
        Settings.AccountLockoutTimeSpan));

与以下版本不同,此版本现在和现在都可以一次解析ConnectDbContext依赖项:

This resolves ConnectDbContext dependencies once, here and now, unlike the following version:

Container.RegisterType<ConnectUserManager>(
    new PerRequestLifetimeManager(),
    new InjectionFactory(
        container => new ConnectUserManager(
            container.Resolve<ConnectDbContext>(),
            container.Resolve<ITemplateManager>(),
            Settings.MaxFailedAccessAttemptsBeforeLockout,
            Settings.AccountLockoutTimeSpan)));

每次正确都能正确解决依赖关系,会构建一个新的ConnectUserManager.

which correctly resolves dependencies every time a new ConnectUserManager is constructed.

这篇关于ASP.NET Identity的UserManager可以缓存用户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆