首页 > 解决方案 > 从 .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>();
    }

标签: asp.net-coreasp.net-identityidentityserver4

解决方案


对于另一种选择,您可以实现自己的扩展来获得UserManager类似的

public static class HttpContextExtension
{
    public static UserManager<TUser> GetUserManager<TUser>(this HttpContext context) where TUser: class
    {
        return context.RequestServices.GetRequiredService<UserManager<TUser>>();
    }
}

推荐阅读