【问题标题】:GetUserManager from OWIN in .net core从 .net 核心中的 OWIN 获取用户管理器
【发布时间】:2019-10-02 13:37:30
【问题描述】:

我正在将现有的 .NET Class 项目迁移到 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>();
    }

【问题讨论】:

  • 在制作自己的问题之前,请务必检查类似问题。奇怪的是,同样的问题在 3 小时前才被问到:stackoverflow.com/questions/58097042/…。我会将此作为骗子关闭,但目前不能,因为它没有被赞成或接受的答案。

标签: asp.net-core asp.net-identity identityserver4


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多