I thouht it would be a simple thing to add SimpleMembersihp to an MVC4 web. Not so. The templated code (e.g. C#) is well-suited to supporting it, but web.config is mostly agnostic-- devoid of elements that would configure any particular security mechanism. I was following Scott Allen's MVC4 tutorial on Pluralsight-- that mixed aspnet-Membership with EF stuff. Maybe I missed something, but C# membership-classes were not interacting with aspnet-Membership, they were doing SimpleMembership. So, I removed aspnet-Membership, but now I cannot get PackageManager to 'update-database'. It complains "You must call the WebSecurity.InitializeDatabaseConnection method...". So, I added an _AppStart.cshtml file with the call, but PM seems unaware. _AppStart.cshtml:
@using WebMatrix.WebData;
@{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);
}
Web.config:
<appSettings>
<add key="enableSimpleMembership" value="true" />
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<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
Configuration.cs:
protected override void Seed(eManager.Web.Infrastructure.DepartmentDb context)
{
context.Departments.AddOrUpdate(d => d.Name,
new Department() { Name = "Engineering" },
new Department() { Name = "Sales" },
new Department() { Name = "Shipping" },
new Department() { Name = "Human Resources" }
);
SimpleRoleProvider roles = new WebMatrix.WebData.SimpleRoleProvider();
SimpleMembershipProvider membership = new SimpleMembershipProvider();
if (!roles.RoleExists("Admin"))
{
roles.CreateRole("Admin");
}
//if (membership.GetUser("ej", false) == null)
//{
// membership.CreateUserAndAccount("ej", "FluffyBunny@1", false);
// string[] u = { "ej" };
// string[] r = { "Admin" };
// roles.AddUsersToRoles(u, r);
//}
}
The role "Admin" is not created.?
Try adding the code
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", true);
To your Application_Start in global.asax.cs
_appStart.cshtml
is not used in MVC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With