Instantiate Identity ApplicationUserManager in Class Library

房东的猫 提交于 2020-01-05 01:44:32

问题


One of the things I do not like about Identity is that the UserManagers have to be instantiated in the controller that you wish to get user information for. In my project, I moved all the business logic into a DAL class library and wanted the Identity to be there as well.

I have already moved all the identity classes into the DAL class library (ApplicationRole, ApplicationUser, ApplicationRoleManager, ApplicationSignInManager, ApplicationUserManager, EmailSerivce, SmsService)

My DataContext inherits from IdentityDBContext

public class DataContext : IdentityDbContext<ApplicationUser>
{
    public DataContext()
        : base("DefaultConnection", throwIfV1Schema: false) 
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

    public static DataContext Create()
    {
        return new DataContext();
    }

    static DataContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, DataContextMigrationsConfiguration>());   
    }

So I have access to user and roles via the DB context

internal class UserRepository
{
    internal ApplicationUser GetUserById(string id)
    {
        using(var db = new DataContext())
        {
            return db.Users.Where(a => a.Id == id).FirstOrDefault();

        }
    }
}

The problem is I do not have access to methods that the ApplicationUserManager exposes. I am trying to Access the ApplicationUserManager from within my user repository but do not know how to instantiate the ApplciationUserManager class.

In the controllers, its instantiated as follows

    private ApplicationUserManager _userManager;

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

However, in the repository in the DAL class library, I do not have access to the OwinContext to get the user manager.

How can I access the ApplicationUserManager from within a Class Library?


回答1:


Visual studio's default MVC template is using the below code to create the ApplicationUserManager instance.

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

It might be possible to instantiate it in your class library if you can provide it with the ApplicationDbContext (DataContext in your case) instance which is being retrieved from OwinContext

e.g.

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new DataContext()));

and also you might have to find some alternatives to instantiate ApplicationUserManager class properties like UserValidator, PasswordValidator etc.

I hope this could give you some idea to proceed!




回答2:


Thanks to Sivaram K this is how i was able to access the Application Manager.

internal ApplicationUser GetUserById(string id)
    {
        using(var db = new DataContext())
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(db));

        }

        using(var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new DataContext())))
        {

        }
    }


来源:https://stackoverflow.com/questions/42556258/instantiate-identity-applicationusermanager-in-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!