Register an "admin" user when running the c# mvc application

Asked

Viewed 129 times

1

good afternoon, I need some idea for a web system to automatically create a user Admin as soon as I run(open) the application.

that is, when running the application with a zeroed database, so that I do not need to register any user before logging in, the system already generates that user automatically when running.

am using Asp net MVC, in visual Studio 2017.

  • Unfortunately, I can’t comment. Note: That in a database the information will not be saved in memory and disappear once you close the application. Well.. Creating a user automatically once you open the application is something simple, but what do you want to happen when you close the application? User to be dropped? User to continue, for the next action of opening the application?

2 answers

1

If you’re working with Code First can use Entity Seed. To enable it to perform on Nuget: Enable-Migrations

 protected override void Seed(ContosoUniversity.DAL.SchoolContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }

Source: https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

1

  • This link may be a good suggestion, but your reply will not be valid if one day the link crashes. In addition, it is important for the community to have content right here on the site. It would be better to include more details in your response. A summary of the content of the link would be helpful enough! Learn more about it in this item of our Community FAQ: We want answers that contain only links?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.