Asked by:
seeding admin user and role if doesnt exist on startup

Question
-
User761356933 posted
Hi guys, I am new to this so sorry for the multiple posts.
I need to seed the admin user on startup if it doesn't exist already and assign the admin role ( also create this role if it doesn't exist) - the following code seems to work just fine but is this the correct way to do it?
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Owin; using Owin; using PrincePortalWeb.Models; using System.Data.Entity; [assembly: OwinStartupAttribute(typeof(PrincePortalWeb.Startup))] namespace PrincePortalWeb { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } public class IdentityRoleHelper { internal static void DefaultAdminUserandRole(ApplicationDbContext context) { var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); //if admin doesnt exist and admin role doesnt exist then add to db if (!roleManager.RoleExists("Admin")) { var roleresult = roleManager.Create(new IdentityRole("Admin")); } if (userManager.FindByName("Admin") == null) { var user = new ApplicationUser() { UserName = "Admin", supplierId = 0, supplierName = "Admin" }; IdentityResult result = userManager.Create(user, "superPRINCE"); if (result.Succeeded) { var userid = user.Id; //add to user role as all contact added will be user only i.e for supplier userManager.AddToRole(userid, "Admin"); context.SaveChanges(); } } } } } }
Tuesday, July 3, 2018 1:55 PM
All replies
-
User-330142929 posted
Hi BulSky28,
According to your description and codes, you want to initialize Asp.net Identity with setting additional user information.
Generally speaking, we initialize the database through overriding the seed method of yourdbcontext which inherits form dbcontext.
I have made a demo, wish it is useful to you.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } static ApplicationDbContext() { Database.SetInitializer<ApplicationDbContext>(new IdentityDbInit()); } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } public class IdentityDbInit : DropCreateDatabaseAlways<ApplicationDbContext> { protected override void Seed(ApplicationDbContext context) { PerformInitialSetup(context); base.Seed(context); } public void PerformInitialSetup(ApplicationDbContext context) { using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context))) { using (var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context))) { //system Role if (!roleManager.RoleExists("admin")) { roleManager.Create(new IdentityRole("admin")); } if (!roleManager.RoleExists("expert")) { roleManager.Create(new IdentityRole("expert")); } if (!roleManager.RoleExists("enterprise")) { roleManager.Create(new IdentityRole("enterprise")); } var user = new ApplicationUser() { UserName = "admin", Name = "admin", Phone = "00000000000" }; if (userManager.Create(user, "000000") != IdentityResult.Success) { throw new Exception("failed"); } var expert = new ApplicationUser() { UserName = "expert", Name = "expert1", Phone = "15888888888" }; if (userManager.Create(expert, "000000") != IdentityResult.Success) { throw new Exception("Failed"); } var enterprise = new ApplicationUser() { UserName = "enterprise", Name = "enterprise1", Phone = "15888888888" }; if (userManager.Create(enterprise, "000000") != IdentityResult.Success) { throw new Exception("Failed"); } IdentityResult result = userManager.Create(new ApplicationUser() { UserName = "a", Name = "dfsd", Phone = "ff" }, "123456"); if (result.Succeeded) { var useid = user.Id; } // role userManager.AddToRole(user.Id, "admin"); userManager.AddToRole(expert.Id, "expert"); userManager.AddToRole(enterprise.Id, "enterprise"); context.SaveChanges(); } } } }
It should be mentioned here that the static constructor will be executed first. Then run the database initialization.
Result.
Here is the relevant link, wish it is useful to you.
https://forums.asp.net/t/2135919.aspx?Seed+Users+and+Roles+in+an+ASP+NET+MVC+Application
Feel free to let me know if you have any question.
Best Regards
Abraham.
Wednesday, July 4, 2018 11:08 AM