Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create default user/admin when website initialize - Identity

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.

like image 937
saf21 Avatar asked Sep 28 '16 03:09

saf21


People also ask

How do you create an identity user?

To create Users in ASP.NET Core Identity you will need to create a Model Class. So create a class called User. cs inside the Models folders. Users are managed through the UserManager<T> class, where T is the class chosen to represent users in the database.

What is UserManager in asp net core?

The ASP.NET Identity UserManager class is used to manage users e.g. registering new users, validating credentials and loading user information. It is not concerned with how user information is stored. For this it relies on a UserStore (which in our case uses Entity Framework).


1 Answers

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 = "[email protected]";
    admin.UserName = "[email protected]";

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

    // Add code to initialize context tables

    base.Seed(context);
}

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.

like image 102
jegtugado Avatar answered Sep 28 '22 00:09

jegtugado