Jon Galloway has an overview of the new membership features in ASP.NET MVC 4. The Internet project template moves away from the core membership providers of ASP.NET and into the world of SimpleMembershipProvider and OAuth.
There is quite a bit I could write about the new features and the code generated by the Internet project template, but for this post I just want to cover a scenario I've demonstrated in the past - seeding the roles and membership tables. If you are using Entity Framework code-first migrations it's relatively easy to add some code to the Seed method of the migrations configuration to populate the membership tables with some initial roles and users. Just remember every update-database command will call the Seed method, so you have to write the code to make sure you don't try to create duplicate data.
First, the new project template creates an MVC 4 Internet application without any provider configuration, but for the membership features to work properly during a migration, it appears you need at least some configuration. The following code makes sure the SimpleMembershipProvider and SimpleRolesProvider are in place.
<roleManager enabled="true" defaultProvider="simple">
<providers>
<clear/>
<add name="simple" type="WebMatrix.WebData.SimpleRoleProvider,
WebMatrix.WebData"/>
</providers>
</roleManager>
<membership defaultProvider="simple">
<providers>
<clear/>
<add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider,
WebMatrix.WebData"/>
</providers>
</membership>
Then inside the Seed method of the DbMigrationsConfiguration<T> derived class, you can have:
protected override void Seed(MovieDb context)
{
//context.Movies.AddOrUpdate(...);
// ...
SeedMembership();
}
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = (SimpleRoleProvider) Roles.Provider;
var membership = (SimpleMembershipProvider) Membership.Provider;
if (!roles.RoleExists("Admin"))
{
roles.CreateRole("Admin");
}
if (membership.GetUser("sallen",false) == null)
{
membership.CreateUserAndAccount("sallen", "imalittleteapot");
}
if (!roles.GetRolesForUser("sallen").Contains("Admin"))
{
roles.AddUsersToRoles(new[] {"sallen"}, new[] {"admin"});
}
}
Comments
Why is this security stuff so linked to Web ?
There is lack of documentation. it seems mvc uses localdb, code migration and every stuff working properly, but chaging the connection string to any sqlserver or sqlexpress almost breaks everything. I think there has to be some sort of webconfig element to change it to sql to make it work properly. So take your time and if you can do a comprehensive work on new membership stuff would be awesome. And getting webmatrix-s exception error messages in the mvc project makes me laugh, and its really confusing. For the first time when I get InitializeDatabase exception with _appStart.cshtml error message, I look in the project find that file. Its too funny I know but it was the message. No doc, no info, nothing. MVC4 membership is a mess and damn confusing. By the way i m using msdn copy of ultimate version. Thanks!
No user table found that has the name "Usuario".
My Configuration class:
protected override void Seed(CreditoImobiliarioContext context)
{
base.Seed(context);
WebSecurity.InitializeDatabaseConnection("Data","SimpleProvider", "Usuario", "Id", "Email", false);
if (!WebSecurity.UserExists("ridermansb@bindsolution.com"))
WebSecurity.CreateUserAndAccount("ridermansb@bindsolution.com", "123456", new { Nome="Riderman de Sousa Barbosa" });
}
brgds!
@riderman: did you already have the table created?
@osman: still haven't had a chance yet. feel free to email me directly.
Tx
@Dave: what is the error message?
Any chances that you would update your Pluralsight video ASP.NET MVC 4 Fundamentals part two on authorization and security to reflect the fact that the default config is now oauth? I'm an ASP.NET MVC rookie, and I got stuck when you started the authorization stuff. My web.config has a lot of oauth, but no membership tags..