4

I'm attempting to use the Simple Membership Provider with MVC 4 as "by the book" as possible. Here is the current scenario:

-- I've been using Jon Galloway's blog post on the topic here.

1) I'm aware this thing is wired via Entity Framework. I did notice, however, that when I added properties to the UserProfile class, they didn't appear in the table automatically when it was generated. Is this due to the database already being generated (tables were not present)? I manually added the fields and it was functional, but would be nice to know the "gotchas" that would result in the fields not being automatically created.

2) As far as the roles, it seems that it is geared primarily towards a global permission type thing (ie user is a User, Admin, etc.). In the event that you want to make it handle at a project level (ie admin for project1, user for project2), what modifications need to be made?

etc) Is there an article that really goes into detail in regards to best practices on how to extend it?

Robert
  • 1,745
  • 5
  • 34
  • 61
  • you should find many posts on this topic. eg http://stackoverflow.com/questions/14672205/code-first-membership-provider/14672293#14672293 – phil soady Feb 05 '13 at 16:52
  • What exactly do you want to do. Use SimpleMembership yet extend the Db ? – phil soady Feb 05 '13 at 16:53
  • The UserProfile object had UserId and UserName. I added Email, Name (for full name), and IsLockedOut. For whatever reason, those properties wouldn't create the fields in the database when generating the tables. I was able to work around it by creating those columns manually, just curious if there is a reason they don't generate (can't view source on the WebSecurity methods which makes sense). The roles table is id and role, but trying to switch it to id, project, role. Currently thinking I'll need to avoid the built in Role add/check functions and build checks but rather go with convention. – Robert Feb 05 '13 at 17:22

1 Answers1

5

1)I find the UserProfile table part of Simple Membership a little complicated (in a good way), but it has worked great for my apps.

The convention for Simple Membership is to create a UserProfile table named "UserProfile" with two fields, UserId and UserName. You can configure a different table name or different UserId and UserName field names for the UserProfile by modifying the WebSecurity.InitializeDatabaseConnection() line in Filters/InitializeSimpleMembershipAttribute.cs. You can create a UserProfile table with additional fields and it will be used by Simple Membership if Simple Membership finds that table the first time it runs. Under the default configuration, the first time your app runs SimpleMembership will create the database tables including whatever UserProfile table details specified in Filters/InitializeSimpleMembershipAttribute.cs.

So, the trick is to create the UserProfile table you want (including all the fields you want in that table) before the first call to Simple Membership. This could be created by EF Migrations or created by a database script or even created manually in SSMS.

If you want to dig into the Simple Membership code, see http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/WebSecurity.cs and http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleMembershipProvider.cs.

2)I agree with your point about roles and global permissions. Maybe you could use AddUsersToRoles and RemoveUsersFromRoles (in http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleRoleProvider.cs) to modify a user's roles at login according to the project they use.

etc)I don't know of a good article on extending Simple Membership, but in principle Simple Membership extends the Extended Membership Provider which extends ASP.NET Membership Provider. You should be able to jump in at an appropriate point.

EDIT in response to Robert's comment:

As a direct answer to why Entity Framework did not create the columns added to the UserProfile class, this happens when the UserProfile table was already created by the SimpeMembership initialization before the app-specific table creation ran. The reason is SimpleMembership has an inbuilt definition of the UserProfile table that is used anytime SimpleMembership creates that table. The timing of the UserProfile table creation is important, so there is a need to make sure the app-specific tables are created before the SimpleMembership initialization runs.

  • 1
    I think this satisfactorily answers the questions. There a couple loose ends (why Entity Framework isn't creating my columns) but all in all, very pleased with the response (primarily due to the inclusion of the related source code). – Robert Feb 06 '13 at 18:32