user6095576
user6095576

Reputation: 45

GetUserManager from OWIN in .net core

I'm migrating an existing .NET Class project to Core. Here we are trying to get current usermanager from owincontext, Can you please help how we can acheive this in .NET core?

I've tried using HttpContext but they no longer have OWINContext or 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>();
    }

Upvotes: 0

Views: 502

Answers (1)

Edward
Edward

Reputation: 29976

For another option, you could implement your own extensioin to get the UserManager like

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

Upvotes: 1

Related Questions