0

I am having some troubles trying to determine where the best place is to create users and roles for an MVC application. Essentially what I want to do is create an admin role and a user role that is already created BEFORE my application gets the the Home Controller.

Now the one thing I'm experiencing is that I added the proper code in my Initializer.cs file but when my application runs it goes to go home controller first and when I debug the code it never hits my Initializer.cs since at this point I haven't access anything regarding my DataContext. I was unsure exactly what code to include in this since it spans multiple files or if my description is enough for comprehension but I can include code if required.

To make it short and sweet, my goal is to create two roles and add users to the roles BEFORE it hits my HomeController. Thanks for any help offered !!

bradc14
  • 113
  • 2
  • 10

4 Answers4

0

If you use Entity Framework to store users/roles you could seed the database

http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx

but what might even be better is write an install controller that you execute upon deployment to seed your admin user (and make sure it runs only if the admin user doesn't exist yet) the call to this controller could even be automated by your install scripts

later you could use this same place for any migrations you might need to do.

Batavia
  • 2,497
  • 14
  • 16
  • I had one idea that I was going to try which was to create the roles and users by calling a method BEFORE it returned the Home Controller View. I'm not sure if this is kind of what you are suggesting ? Also I wanted to do the seed method but my issue is that it never launches the Initialzer I created before it loads the HomeController so it cant even get to it. Im not sure if I am missing something. Its a bit confusing just since I was given the project pre-made so im trying to piece it together. – bradc14 Aug 02 '14 at 22:22
  • if you use the entity framework seeding it 'should' seed automatically when you use it. but easier is just make a deployController that runs the code you need just once and call it manually (or automatically from deployment scripts) – Batavia Aug 06 '14 at 20:46
0

There is an excellent article from Microsoft which covers all the major points regarding this topic.

Main points of note are the use of Entity Framework Migrations and a database seed method, along with using the [Authorize(Roles = "exampleRole")] Directive.

Claies
  • 22,124
  • 4
  • 53
  • 77
0

What I would do is to take a look at this: https://github.com/thinktecture/Thinktecture.IdentityManager

Its an application that you can run locally and just configure your db connection string and then you can add users and claims to your db like in the old days with the ASP.NET WebSite Administration tool.

The application is simple to use and you then has a easy tool to update and add users to your application and skip that part of the application until you really need it and focus on more productive things instead.

Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283
0

Okay so I found what I wanted to do. I think what you guys were trying to do was a bit more complicated then I needed to use.

using (var context = new DataContext())
{
    context.Database.Initialize(force: true);
}

I included this in my Global.asax.cs file inside the Application_Start() method and it did exactly what I was trying to accomplish. Hope this helps for anyone looking for something similar.

bradc14
  • 113
  • 2
  • 10