【问题标题】:Invalid Username or password on Seeded User Account MVC5种子用户帐户 MVC5 上的用户名或密码无效
【发布时间】:2015-03-05 15:42:30
【问题描述】:

我正在开发一个新的 MVC5 应用程序,该应用程序专门用于测试这一点。

我有 两个 数据库上下文。 ApplicationDbContext 对应的configuration.cs 如下:

using FB.DOMAIN.Authentication;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity.Migrations;

namespace FB.DAL.Migrations.ApplicationDbContext
{
    internal sealed class Configuration : DbMigrationsConfiguration<DataContexts.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsDirectory = @"Migrations\ApplicationDbContext";
        }

        protected override void Seed(DataContexts.ApplicationDbContext context)
        {
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new IdentityRole("Admin"));
            }

            var user = new ApplicationUser { UserName = "james@world.com" };


            if (userManager.FindByName("james@world.com") != null) return;

            var result = userManager.Create(user, "Password123!");

            if (result.Succeeded)
            {
                userManager.AddToRole(user.Id, "Admin");
            }
        }
    }
}

上面的代码不应该用用户为我的数据库播种吗?可惜没有!当我查询AspNetUsers 表时,它是空白的!

我做错了什么? :(

【问题讨论】:

标签: entity-framework-6 entity-framework-migrations asp.net-mvc-5.2


【解决方案1】:

我无法让两个不同的上下文对话。所以我关注了LukeP的solution here

一个包含我所有相关领域逻辑的单一上下文。

【讨论】:

    猜你喜欢
    • 2015-05-31
    • 2016-08-22
    • 2013-11-15
    • 2016-11-14
    • 2013-08-28
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多