网站初始化时创建默认用户/管理员-身份 [英] Create default user/admin when website initialize - Identity

查看:98
本文介绍了网站初始化时创建默认用户/管理员-身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net区域非常菜鸟.因此,我的问题也许可以在Internet上的任何地方得到解答,但是我都不明白流程的创建.这是问题.

I'm very noob in asp.net area. So my question maybe got answered anywhere in the internet but neither of them I understand the flow creation. Here is the question.

我正在尝试创建一个简单的网站项目",当该网站启动时,如果该表尚不存在,则应在数据库中创建所有必需的身份表.然后,我将角色添加到表中,并将一个用户设置为该表的超级管理员.

I'm trying to create simple 'Website project', when this site was up, it should creating all necessary identity table inside database if the table not exist yet. Then I'll add up role to the table and set one user as super admin to this table.

任何人都可以帮助我解决这个问题,或分享有关此问题的任何链接.

Can anyone help me regarding this or share any link regarding this question.

推荐答案

我假设您正在使用ASP.NET MVC.

I am assuming you are using ASP.NET MVC.

您可以使用数据库初始化为您的数据库设定种子.

You can use Database Initialization to seed your database.

public class MyDBInitializer : CreateDatabaseIfNotExists<ApplicationDBContext>
{
    protected override void Seed(ApplicationDBContext context)
    {
        base.Seed(context);
    }
}

在种子方法上,您可以填充数据库并为应用程序创建默认用户.

On the seed method you can populate your database and create a default user for your application.

protected override void Seed(ApplicationDBContext context)
{
    // Initialize default identity roles
    var store = new RoleStore<IdentityRole>(context);
    var manager = new RoleManager<IdentityRole>(store);               
    // RoleTypes is a class containing constant string values for different roles
    List<IdentityRole> identityRoles = new List<IdentityRole>();
    identityRoles.Add(new IdentityRole() { Name = RoleTypes.Admin });
    identityRoles.Add(new IdentityRole() { Name = RoleTypes.Secretary });
    identityRoles.Add(new IdentityRole() { Name = RoleTypes.User });

    foreach(IdentityRole role in identityRoles)
    {
         manager.Create(role);
    }

    // Initialize default user
    var store = new UserStore<ApplicationUser>(context);
    var manager = new UserManager<ApplicationUser>(store);
    ApplicationUser admin = new ApplicationUser();
    admin.Email = "admin@admin.com";
    admin.UserName = "admin@admin.com";

    manager.Create(admin, "1Admin!");
    manager.AddToRole(admin.Id, RoleTypes.Admin); 

    // Add code to initialize context tables

    base.Seed(context);
}

在Global.asax.cs上,您应该注册数据库初始化程序

And on your Global.asax.cs you should register your database initializer

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    Database.SetInitializer(new MyDBInitializer());
}

请注意,有不同类型的数据库初始化策略,其行为将取决于数据库初始化程序.请检查上面的链接.

Note that there are different types of database initialization strategies whose behavior will depend on your database initializer. Do check the link above.

这篇关于网站初始化时创建默认用户/管理员-身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆