SimpleMemership CreateUserAndAccount Customization

怎甘沉沦 提交于 2019-12-02 22:25:14

Try like this:

private void SeedMembership()
{
    WebSecurity.InitializeDatabaseConnection(
        "MafiaContext",
        "UserProfile", 
        "UserId", 
        "UserName", 
        autoCreateTables: true
    );

    if (!Roles.RoleExists("Administrator"))
    {
        Roles.CreateRole("Administrator");
    }

    if (!WebSecurity.UserExists("drew"))
    {
        WebSecurity.CreateUserAndAccount(
            "drew", 
            "password",
            new { Email = "myemail@gmail.com" },
            false
        );
    }

    if (!Roles.GetRolesForUser("drew").Contains("Administrator"))
    {
        Roles.AddUsersToRoles(new[] { "drew" }, new[] { "Administrator" });
    }
}

Here's a nice tutorial about the SimpleMembershipProvider you might consider going through.

Since last parameter of SimpleMembershipProvider.CreateUserAndAccount method should be IDictionary<string, Object> your code should be rewritten as follows:

        membership.CreateUserAndAccount(
            "drew", 
            "password",
            new Dictionary<string, object> { new { "Email", "myemail@gmail.com" } }
        );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!