How do i create an instance of UserManager

后端 未结 2 1377
花落未央
花落未央 2021-01-13 15:29

I am trying to learn how the new asp.net identity 2.0 works, but with little documentation I am hitting quite a few stumbling blocks.

I have this code below based of

相关标签:
2条回答
  • 2021-01-13 16:04

    This worked me. Seems you have to create own usermanager and userstore if you create custom users and roles. This is the derived UM(you can create the same way the rolemanager too):

    public class ApplicationUserManager : UserManager<ApplicationUser, string>
        {
            public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
                : base(store)
            {
    
            }
    
    
        }
    public class ApplicationUserStore : UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>
        {
            public ApplicationUserStore(ApplicationDbContext context)
                : base(context)
            {
            }
        }
    

    Then create the UserManager:

    ApplicationUserManager um = new ApplicationUserManager(new ApplicationUserStore(new ApplicationDbContext()));
    
    0 讨论(0)
  • 2021-01-13 16:05

    try this:

    UserManager = new UserManager<ApplicationUser,string>(new UserStore<ApplicationUser, CustomRole, string, CustomUserLogin, CustomUserRole, CustomUserClaim>(new myDbContext()));
    

    note I use a different construction for UserManager, I added string as a second type that you use in your code for ApplicationUser primary key

    Since you implemented custom user/roles/etc the way you did, you'll need to use UserManager as UserManager<ApplicationUser,string> throughout your code to pass in the type for User PK as a string.

    0 讨论(0)
提交回复
热议问题