簡體   English   中英

Rolemanager在Identity 2.0中始終為null

[英]Rolemanager always null in Identity 2.0

我在那里看到了這個問題。 但答案對我不起作用。

我創建了一個空的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)

我試試這個,問題就解決了。

在“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>()返回null

剛剛完成了這項工作,並提出:

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異步方法時遇到了一些問題,並建議同步執行所有操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM