Successfully implementing custom UserManager<IUser> in Identity 2.0

10,631

Solution 1

I had several errors, but the main one was that my Startup class looked like :

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseWebApi(new WebApiConfig());

        app.CreatePerOwinContext(MyDbContext.Create);
        app.CreatePerOwinContext<MyUserManager>(MyUserManager.Create);
    }
}

Of course, the OWIN pipeline will plug my dependencies after firing off the WebAPI calls. Switching those around was my main problem.

In addition, I finally found this great guide way down in my Google search. It answers pretty much everything I was getting at. I needed to implement a concrete class that was a UserManager<MyUser,int>

I hope this is able to help someone later on.

Solution 2

Are you using MyIdentity.User or MyIdentity.MyUser? You have two different objects passed into the TUser parameter in your examples.

I tested your implementation and did not have issues retrieving the registered instance of the UserManager.

To fully customize your Users and Roles, you will need to end up writing a new implementation of the UserManager in order to get your Authentication, Claims, Roles, method calls, etc to work.

If you are trying to learn how Identity works, I would suggest working with the default implementation of UserManager and IdentityUser. Get that to work first.

Here is a good tutorial

Eventually, create your own users implementing IdentityUser. All of the UserManager functions will work and you will have access to your new properties. The additional properties will automatically be built into the AspnetUser table generated by the IdentityDbContext.

public MyIdentityUser : IdentityUser
{
   public int Id {get; set;}
   public string CustomUserId {get; set;}
   public string UserId { get; set; }
}

Or you can start building your custom MyIdentityUser with foreign key references to other objects.

public MyIdentityUser : IdentityUser
{
   public virtual UserProperties Properties { get; set; }
}

Does this help any?

Share:
10,631
Jonesopolis
Author by

Jonesopolis

.NET Developer / Dad #SOreadytohelp

Updated on June 17, 2022

Comments

  • Jonesopolis
    Jonesopolis almost 2 years

    I'm trying to navigate the black hole that is the custom implementation of Identity Membership. My goal right now is simply to get this line from my ApiController to correctly retrieve my UserManager:

    public IHttpActionResult Index()
    {
        var manager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager<MyUser,int>>();
        //manager is null
    }
    

    Here's my setup. In my Startup's Configuration I set up the WebApi and add my OwinContext:

    app.CreatePerOwinContext<UserManager<MyUser,int>>(Create);
    

    My Create method:

    public static UserManager<User,int> Create(IdentityFactoryOptions<UserManager<MyUser,int>> options, IOwinContext context)
    {
       return new UserManager<MyUser,int>(new UserStore(new DbContext()));
    }
    

    the rest is the most basic implementation I can make.

    MyUser:

    public class MyUser : IUser<int>
    {
        public int Id { get; set; }
        public string UserName { get; set; }
    }
    

    MyUserStore:

     public class MyUserStore : IUserStore<MyUser,int>
     {
        private IdentityDbContext context;
        public MyUserStore(IdentityDbContext c)
        {
           context = c;
        }
    
        //Create,Delete, etc implementations
     }
    

    MyDbContext:

     public class MyDbContext : IdentityDbContext
     {
         public MyDbContext() : base("Conn")
         {
    
         }
     }
    

    This is all for the sake of learning how Identity works, which I'm pretty convinced no one actually knows. I want to be able to fully customize my Users and Roles eventually, avoiding Microsoft's IdentityUser.

    Again, my issue right now is that in my controller, I am getting null when trying to retrieve my UserManager.

    Any and all help is greatly appreciated.