SimpleMemership CreateUserAndAccount Customization
Asked Answered
N

2

11

I am trying to add a new property to the UserProfile class in my model

public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email Address")]
    public string Email { get; set; } \\this is the new property
    public virtual IList<Game> Games { get; set; }
}

I am trying to add it to my seed method in my Configurations.cs file

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

        var roles = (SimpleRoleProvider)Roles.Provider;
        var membership = (SimpleMembershipProvider)Membership.Provider;

        if (!roles.RoleExists("Administrator"))
        {
            roles.CreateRole("Administrator");
        }
        if (membership.GetUser("drew", false) == null)
        {

            membership.CreateUserAndAccount(
                "drew", 
                "password", false,
                new { Email = "[email protected]" }); \\ this line errors
        }
        if (!roles.GetRolesForUser("drew").Contains("Administrator"))
        {
            roles.AddUsersToRoles(new[] { "drew" }, new[] { "Administrator" });
        }
    }

However, I receive an error:

Error 2 Argument 4: cannot convert from 'AnonymousType#1' to 'System.Collections.Generic.IDictionary<string,object>' C:\Code\Mafia\Mafia.EF\Migrations\Configuration.cs 65 21 Mafia.EF

I have followed examples, such as: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

But I'm coming up with nothing... My WebMatrix.WebData version is v4.0.30319

Any thoughts?

Nearly answered 26/1, 2013 at 22:32 Comment(0)
U
20

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 = "[email protected]" },
            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.

Understructure answered 26/1, 2013 at 22:43 Comment(2)
Thanks Darin! I was just coming back to post an edit saying I had realized I was using the casted version of the Membership.Provider and not WebSecurity itself. Edit: I should mention I linked that very same tutorial in my post because it IS good :)Nearly
Hi Darin - I've been using your answer here for some time but I'm having trouble with a special case - if you have a moment, any input would be awesome: #19775800Underbelly
F
0

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", "[email protected]" } }
        );
Fadge answered 26/1, 2013 at 22:49 Comment(2)
Using the same casted version of (SimpleMembershipProvider)Membership.Provider will not allow that to work in any fashion - I just tried. It could be a syntax thing, and unfortunately, I'm not an expert in C#. More of a beginning intermediary level... Maybe someone else can provide another option if one needs to use the (SimpleMembershipProvider)Membership.Provider type.Nearly
Sorry, I provided wrong code snippet, in snippet membership variable should be used instead of WebSecurity. Darin's answer is far better, my comment is just a clarification why previous version of code I provided was not working.Fadge

© 2022 - 2024 — McMap. All rights reserved.