0

我使用具有自动迁移功能的 EntityFramework 5.0 Code First 和具有简单成员资格的 MVC4 开始了一个新项目。

我用以下内容修改了 Configuration.cs:

        protected override void Seed(UsersContext context)
        {
            if (!WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "UserID", "Username", true);
            }
            Console.WriteLine("Initialized websecurity");
            CreateUser("admin");
            CreateUser("radu");
            CreateUser("mariana");
        }

        private static void CreateUser(string username)
        {
            if (!WebSecurity.UserExists(username))
            {
                WebSecurity.CreateUserAndAccount(username, "123456");
            }
            else
            {
                Membership.DeleteUser(username);
                WebSecurity.CreateUserAndAccount(username, "123456");
            }
        }

Web.config 条目如下所示:

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider">
      <providers>
        <clear/>
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
      </providers>
    </roleManager>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <clear/>
        <add name="SimpleMembershipProvider"
             type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>
      </providers>
    </membership>

错误信息:提供的用户名或密码不正确。

不幸的是,在成功迁移后,当我尝试使用插入 db 的用户之一登录时,我收到无效的登录消息。为什么会这样,我应该如何解决这个问题?

4

1 回答 1

0

我发现了问题:

protected override void Seed(Handmade.Web.Models.UsersContext context) {
    if (!WebSecurity.Initialized) {
        //WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "UserID", "Username", true); 
        // I used wrong init for seed method
        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);
    }
    Console.WriteLine("Initialized websecurity");
    CreateUser("admin");
    CreateUser("radu");
    CreateUser("mariana");
}

private static void CreateUser(string username) {
    if (!WebSecurity.UserExists(username)) {
        WebSecurity.CreateUserAndAccount(username, "123456");
    } else {
        Membership.DeleteUser(username);
        WebSecurity.CreateUserAndAccount(username, "123456");
    }
}

还从我使用的过滤器属性更正了 init:

private class SimpleMembershipInitializer
        {
            public SimpleMembershipInitializer()
            {
                //Database.SetInitializer<UsersContext>(null);

                try
                {
                    using (var context = new UsersContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        }
于 2013-05-19T10:52:49.990 回答