Rolemanager在Identity 2.0中始终为null

时间:2014-08-22 03:26:31

标签: c# asp.net asp.net-identity

我在那里看到了这个问题。但答案对我不起作用。

我创建了一个空的asp.net网站。 .NET 4.5

我通过Install-Package Microsoft.AspNet.Identity.Sample -pre

在nuget中安装了示例

我无法运行初始化程序。所以我做了以下

 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();
    }
}

Rolemanager始终为null。我尝试了其他答案,但他们从未奏效。除了上面显示的内容之外,我没有更改样本。那为什么不起作用呢?数据库和表是由tho创建的。 rolemanager在下面使用,并且为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)

2 个答案:

答案 0 :(得分:14)

我试试这个,问题就解决了。

在“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);
        ...
    }
}

只是

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

答案 1 :(得分:2)

刚刚完成了这项工作,并想出了:

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);   
...

这似乎修复了我的Seed方法中的其余问题。我在Create和FindByName中使用RoleManager / UserManager异步方法时遇到了一些问题,并建议同步执行所有操作。