asp.net-core - 从 .net 核心中的 OWIN 获取用户管理器
问题描述
我正在将现有的 .NET类项目迁移到 Core。在这里,我们正在尝试从 owincontext 获取当前的用户管理器,您能否帮助我们如何在 .NET 核心中实现这一点?
我试过使用 HttpContext 但他们不再有 OWINContext 或 GetUserManager()
private CustomStore _myStore;
private MyContext _dbContext;
private AppUserManager _userManager;
private AppRoleManager _roleManager;
public CustomManager(HttpContext ctx)
{
_dbContext = ctx
.GetOwinContext().Get<MyContext>();
_userManager = ctx
.GetOwinContext().GetUserManager<AppUserManager>();
_roleManager = ctx
.GetOwinContext().Get<AppRoleManager>();
}
解决方案
对于另一种选择,您可以实现自己的扩展来获得UserManager
类似的
public static class HttpContextExtension
{
public static UserManager<TUser> GetUserManager<TUser>(this HttpContext context) where TUser: class
{
return context.RequestServices.GetRequiredService<UserManager<TUser>>();
}
}
推荐阅读
- azure - Azure CLI 密钥保管库恢复显示错误消息 - 它有重试策略吗?
- asp.net-mvc - Custom route that match a single specific url
- javascript - Iframe/网页的自定义请求标头
- android - 在水平回收器视图中滚动时获取项目
- java - 如何将某些值插入到某个部分的字符串中
- typescript - TypeScript:如何将元组转换为对象?
- spring - 无法从 Maven 存储库下载 Spring Boot jar
- amazon-web-services - AWS S3 存储桶名称是否有任何可用的策略变量?
- ios - 委托方法 UIDocumentPickerViewController 在模拟器上不起作用
- openssl - OpenSSL:如何从 RFC3161 时间戳回复中提取证书和令牌状态?