【问题标题】:Seeding users with membership provider使用会员提供者播种用户
【发布时间】:2013-05-11 10:01:36
【问题描述】:

我正在尝试使用SimpleMembershipProvider 为一些用户播种。我在 UserProfile 表中添加了几列,例如手机号码。当我尝试使用手机号码添加用户时,编译器告诉我:

The name 'Mobile' does not exist in the current context 

这是课程:

namespace _DataContext.Migrations {
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using WebMatrix.WebData;
    using System.Web.Security;

internal sealed class Configuration : DbMigrationsConfiguration<_DataContext.DataContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(_DataContext.DataContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //

        SeedMembership();
    }

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


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

            if (!roles.RoleExists("Administrator"))
                roles.CreateRole("Administrator");

            if (membership.GetUser("Username", false) == null)
                membership.CreateUserAndAccount("Username", "Pass", false, 
                    new Dictionary<string, object> 
                    { 
                        { Mobile = "+311122334455" }, 
                    });

            /*if (!WebSecurity.UserExists("test"))
                WebSecurity.CreateUserAndAccount(
                    "Username",
                    "password",
                    new { 
                            Mobile = "+311122334455", 
                            FirstName = "test", 
                            LastName = "test",
                            LoginCount = 0,
                            IsActive = true,
                        });
              */
    }
  }
}

如果我使用WebSecurity,一切都会好起来的。

我在这里做错了什么?

【问题讨论】:

    标签: asp.net-mvc-4 ef-code-first entity-framework-migrations


    【解决方案1】:

    这只是你创建Dictionary 的方式,你不能这样做:

    membership.CreateUserAndAccount("Username", "Pass", false,
        new Dictionary<string, object> 
        { 
            { Mobile = "+311122334455" }, // Mobile won't compile here
        });
    

    所以改为使用:

    membership.CreateUserAndAccount("Username", "Pass", false,
        new Dictionary<string, object> 
        { 
            { "Mobile", "+311122334455" }, // Mobile should be the string in the string, object pair
        });
    

    对于它的价值,WebSecurity 与您所做的完全相同,但您不必在代码中指定确切的提供程序。

    【讨论】:

    • 嗨,有道理,我想我也尝试过这个选项,但不确定。但是你对网络安全的意思是什么阻止我指定确切的提供者?
    • @Yustme。它只是将您从具体的提供者实现中抽象出来。它的代码也采用Membership.Provider,但将其转换为所有提供者的公共基类而不是SimpleMembershipProvider(因此对于IoC 和/或DI 可能更好)。除非您非常担心性能,否则我会使用 WebSecurity。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多