【问题标题】:How can I add users to the web application manually?如何手动将用户添加到 Web 应用程序?
【发布时间】:2014-09-25 16:07:45
【问题描述】:

我正在编写一个封闭的 ASP .NET MVC 5.1 应用程序。只有授权的人才能进入。我想从应用程序中删除注册操作。我可以通过在与 Web 应用程序关联的数据库中添加新行来手动添加用户吗?

如何在 Visual Studio 2013 中执行此操作?

【问题讨论】:

    标签: entity-framework visual-studio-2013 authorization asp.net-mvc-5 roles


    【解决方案1】:

    将用户直接添加到数据库表中通常并不容易,因为存在许多相互关联的问题,例如权限、角色和密码哈希。

    但是,可以在代码中“播种”数据库。以下是使用 ASP.NET Identity 的 Seed 函数示例。

    protected override void Seed(ApplicationDbContext context)
    {
        //First, access the UserManager
        var store = new UserStore<ApplicationUser>(context);
        var manager = new UserManager<ApplicationUser>(store);
    
        //Secondly, Create the user account
        var user = new ApplicationUser
        {
            UserName = "ExampleUser",
            UserProfileInfo = new UserProfileInfo
            {
                FirstName = "Example",
                LastName = "User",
                EmailID = "exampleuser@testdomain.com"
            }
        };
    
        //Last, add the user to the database
        manager.Create(user, "password123");
    }
    

    此函数将在下次在 NuGet 包管理器控制台中运行 Update-Database 时运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2017-04-11
      • 2012-01-23
      相关资源
      最近更新 更多