4

I see this question out there. but the answer doesn't work for me.

I created an empty asp.net website. .NET 4.5

I installed the sample in nuget via Install-Package Microsoft.AspNet.Identity.Sample -pre

I could not get the initializer to run. so i the did the following

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        this.Database.Initialize(true);
    }

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        //

    }` `

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

The Rolemanager is always null. I tried the other answers but they never worked. I did not change the sample other than what is shown above. So why doesn't it work? the db and the tables are created tho. the rolemanager is used below and is null.

 public static void InitializeIdentityForEF(ApplicationDbContext db)
    {
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
        const string name = "admin@example.com";
        const string password = "Admin@123456";
        const string roleName = "Admin";

        //Create Role Admin if it does not exist
        var role = roleManager.FindByName(roleName);
        if (role == null)
Joe
  • 721
  • 9
  • 22

2 Answers2

18

I try this, and problem is solved.

in 「App_Start\Startup.Auth.cs」

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and role manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
        ...
    }
}

just

ref HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() return null

Community
  • 1
  • 1
Ken Tseng
  • 181
  • 1
  • 6
  • 3
    if you explain a little bit better why is it actually a solution for the problem, it might help to improve the answer – lp_ Jul 23 '15 at 08:53
  • For me "app.CreatePerOwinContext(ApplicationRoleManager.Create);" was not in the Startup.Auth.cs code so that's why RoleManager was NULL. – Dave Stuart Dec 30 '19 at 18:48
2

Just finished working on this, and came up with:

public static void InitializeIdentityForEF(ApplicationDbContext context)
        {
            context.Configuration.LazyLoadingEnabled = true;

            //var userManager = HttpContext.Current
            //    .GetOwinContext().GetUserManager<ApplicationUserManager>();

            //var roleManager = HttpContext.Current
            //    .GetOwinContext().Get<ApplicationRoleManager>();

            var roleStore = new RoleStore<ApplicationRole, int, ApplicationUserRole>(context);
            var roleManager = new RoleManager<ApplicationRole, int>(roleStore);
            var userStore = new UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>(context);
            var userManager = new UserManager<ApplicationUser, int>(userStore);   
...

This seems to have fixed the remainder of the issues in my Seed method. I had some problems using the RoleManager/UserManager async methods for Create and FindByName and would suggest doing everything synchronously.

goobering
  • 1,547
  • 2
  • 10
  • 24
  • This helped me out except maybe you're on a different version than me or something because mine is more like: `var roleStore = new RoleStore(context);` `var roleManager = new RoleManager(roleStore);` `var userStore = new UserStore(context);` `var userManager = new UserManager(userStore);` – Literate Corvette Dec 09 '21 at 22:57