Search code examples
c#sql-serverasp.net-mvcasp.net-identitycode-first

Store the Controllers name and Actions Name into my Custom Tables that I Add them into Identity in CodeFirst Asp.Net MVC Project


Seniors,
I'm using ASP.NET Identity on my ASP.Net web application and I Add my custom Tables(with their relations) to Identity on my CodeFirst ASP.Net MVC Project.when I Run Project for the first time,the databace is created automatically with the custom tables and relations between them in SqlServer.
Custom Tables :

  1. MvcControllers
  2. ActionsTbls
  3. GroupsTbls
  4. AspNetUser_Action
  5. AspNetUser_Group
  6. Action_Group

For Creating Custom tables,I Add Some Codes to IdentityModels.cs.
IdentityModels.cs :

namespace Admin_Base_SN.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
        public virtual AspNetUser_Action AspNetUser_Action { get; set; }

        public virtual AspNetUser_Group AspNetUser_Group { get; set; }

    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {


        public DbSet<MvcController> MvcController { get; set; }
        public DbSet<ActionsTbls> ActionsTbls { get; set; }
        public DbSet<AspNetUser_Action> AspNetUser_Actions { get; set; }

        public DbSet<GroupsTbl> GroupsTbls { get; set; }
        public DbSet<Action_Group> Action_Groups { get; set; }

        public DbSet<AspNetUser_Group> AspNetUser_Groups { get; set; }
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // one-to-zero or one relationship between ApplicationUser and Customer
            // UserId column in Customers table will be foreign key
            modelBuilder.Entity<ApplicationUser>()
                .HasOptional(m => m.AspNetUser_Action)
                .WithRequired(m => m.ApplicationUser)
                .Map(p => p.MapKey("AspNetUser_Id"));

            modelBuilder.Entity<ApplicationUser>()
                .HasOptional(m => m.AspNetUser_Group)
                .WithRequired(m => m.ApplicationUser)
                .Map(p => p.MapKey("AspNetUser_Id"));

        }

    }
}

What I want
At first,I want to save All the Controllers Names and Actions Names of Project into separated Lists,then insert them into MvcController Table & ActionsTbl Table.This process should be done automatically when I Run Project for the first time.I mean When the Database is Created, the Lists inert to their tables automatically.

  • I think it's better to Add a New Custom Function to the Identity for inserting Lists to the Tables.

I appreciate your efforts in reaching a solution for my problem.


Solution

  • If desired types are Controller and ActionResult (without web api), then you may get controllers and actions by using reflection

            var controllers = Assembly.GetAssembly(typeof(*any type in assembly*))
                .GetTypes()
                .Where(x => x.IsSubclassOf(typeof(Controller)))
                .ToList();
    
            var result = controllers
                .Select(type => new
                {
                    ControllerType = type,
                    Actions = type.GetMethods()
                    .Where(m => m.ReturnType == typeof(ActionResult) || m.ReturnType.IsSubclassOf(typeof(ActionResult))).ToList()
                })
                .ToList();
    

    Since you're using EF you can put all necessary logic into Seed Method and enable Automatic Migration