- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Asp.Net Core 应用程序中有以下中间件,我需要在其中注入(inject) UserManager
。当我尝试查询用户 FindByIdAsync
时,出现以下异常:
ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur is you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ApplicationDbContext'.
<小时/>
我的中间件:
namespace MyApp
{
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
public class MyMiddleware<TUser, TRole>
where TUser : class where TRole : class
{
private readonly RequestDelegate _next;
private readonly UserManager<TUser> _userManager;
private readonly RoleManager<TRole> _roleManager;
private readonly IOptions<IdentityOptions> _optionsAccessor;
public UserIdentityMiddleware(RequestDelegate next, UserManager<TUser> userManager,
RoleManager<TRole> roleManager, IOptions<IdentityOptions> optionsAccessor)
{
_next = next;
_userManager = userManager;
_roleManager = roleManager;
_optionsAccessor = optionsAccessor;
}
public async Task Invoke(HttpContext context)
{
var user = await _userManager.FindByIdAsync("1");
var claimsPrincipal = await new UserClaimsPrincipalFactory<TUser, TRole>(
_userManager, _roleManager, _optionsAccessor).CreateAsync(user);
context.User.AddIdentities(claimsPrincipal.Identities);
await _next(context);
}
}
}
最佳答案
出现这个问题是因为中间件本身是单例的,所以它的内部依赖也需要是单例的,但是像 UserManager 这样的身份类默认是按请求限定范围的,这就是它们在 Startup.ConfigureServices 中注册的方式以及UserManager 的依赖项(例如 dbContext)也按请求限定范围。
通常,每个请求的作用域是您想要这些对象的范围,特别是如果它们也用于其他地方(例如 Controller )。
一种解决方案是不要让中间件采用对 UserManager 的构造函数依赖,您可以仅在需要时从 Invoke 方法内部进行访问,语法如下所示:
var userManager = context.RequestServices.GetService(UserManager<TUser>);
这可能不是完全正确的语法,您可能必须指定实际的用户类型
缺点是这将是服务定位器模式,但它可以解决问题,因为这样你会为每个请求获得一个 UserManager,这就是它应该如何确定范围的
实际上我认为您可以避免服务定位器模式,因为 Invoke 方法是可注入(inject)的,您可以向方法签名添加额外的依赖项,如下所示:
public async Task Invoke(HttpContext context, UserManager<TUser> userManager)
{
...
}
但可能再次需要提供实际类型而不是 TUser
关于asp.net - Usermanager.DbContext 已在中间件中处置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39369054/
我使用以下代码来更改用户的密码: UserManager userManager = new UserManager(new UserStore(new ApplicationDbContext())
我正在尝试实现 Asp.Net Identity 2.0。到目前为止,我在 this blog 的帮助下管理得很好.但我走了一条稍微不同的路。我希望某些数据不属于 User 对象,而是属于新创建的 C
这个问题在这里已经有了答案: How would I run an async Task method synchronously? (25 个回答) 3个月前关闭。 在我的其中之一 IDatabas
我必须为我的单元测试创建 owin 环境,这是创建 Usermanager 对象所必需的。在我目前的情况下,我的 usermanager 为空,因为它没有获取 owincontext。 这是我访问
我有以下代码: var clientId = this.UserManager.FindById(this.User.Identity.GetUserId()).ClientId; 它是我应用程序中
我有一个问题,当我创建用户时,它没有初始化 list<> 类型的属性 - 我收到以下错误: An unhandled exception occurred while processing the r
我有一个应用程序使用 Microsoft.AspNetCore.Identity.UserManager 来管理用户。我使用核心 2.2。 UserManager 是通过我的服务中的内置依赖项注入(i
使用 ASP.NET Core 2。 我正在尝试初始化一些应用程序范围的数据。为此,我创建了一个名为 DbInitializer 的新类。 ,一个名为 IDbInitializer 的接口(inter
我的应用程序的所有身份验证和授权过程都是使用存储过程完成的。我已经编写了一个包含我需要的所有功能的类,例如GetUsers、Login、AddRole、AddMember 等。此外,用于管理用户、角色
UserManager.FindAsync 总能找到匹配项,即使我输入了错误的用户名和密码,它也总是会返回某种用户 ID。 [HttpPost] public ActionResult Mobilel
ASPNET Identity 中的 UserManager 包含 AddPasswordAsync 和 SetPhoneNumberAsync 等函数。 我已经为 FirstName 和 LastN
背景 我正在使用 .Net Core API 来驱动歌词应用程序。用户可以注册、提交艺术家和歌词,并在此过程中获得荣誉/XP 积分。基本上是一个社区驱动的歌词网站。 代码 这是我的 ArtistCon
所以我创建了这个继承自 UserManager 的用户服务,它看起来像这样: /// /// Service for handling users /// public class UserSer
我想做的是添加一个新的管理员用户并为其分配管理员角色。所以..我转到 Configure 方法中的 Startup.cs 类并编写了以下代码: var context = app.Applicatio
我有一个带有自定义电子邮件服务的自定义用户管理器。我在 AccountController 中调用 UserManager.SendEmailAsync() 方法,它只是挂起。我什至尝试将它与无效的
Android 7.1 UserManager类中出现的这个新方法描述here Checks if the calling app is running in a demo user. When ru
我正在使用 FOSUserBundle 并创建了一个自己的模型,其中包含一些新字段(例如 deviceVersion ( string ) 和 deviceType ( string )。 如果我创建
尝试使用 UserManager CreateAsync 方法创建新用户时出现以下错误。我使用的是未修改的 IdentityUser 和 IdentityRole。我有一些 DBSets,它们填充了数
我有一些服务负责在使用 UserManager 时更改用户密码并且一切正常,但是当我想编写一些测试时,它开始在方法 CheckPassword,它主要检查 current (old password)
如何在 ASP.NET Identity 中模拟 UserManager.GetRoles()?我知道它是一个扩展方法,所以我不能直接模拟它,也找不到需要模拟的底层方法/属性。 过去,我能够通过模拟
我是一名优秀的程序员,十分优秀!