Disable MVC 4 SimpleMembership for early development on Azure websites
Early dev using MVC 4 on Azure websites can get stuck on SimpleMembership.
Introduction
If you want to emulate the Login experience into the initial development workflow, you can get stuck on Azure websites with the SimpeMembership framework included in the MVC 4 project template.
Background
Typically the login screen is the first part of a user workflow. Often it is the entryway to a menuing system in a web application. As you start your MVC 4 project, you want to start out with the rich starting point using the Internet Web Application templates. They provide a framework for SimpleMembership identity management and External Identity Providers such as Microsoft Live, Facebook, and others. So, while you want the features later, the WebSecurity framework can get in your way initially.
Using the code
Disable SimpleMembership from MVC 4 Internet Web Project Template
Step 1. – Comment out the “DefaultConnection
” in the web.config file.
Type=XML
<connectionStrings>
<!--
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial
Catalog=aspnet-RadvSampleDaycareApp001-20130508142535;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-RadvSampleDaycareApp001-20130508142535.mdf"
providerName="System.Data.SqlClient" />
-->
</connectionStrings>
Step 2 – Modify InitializeSimpleMembershipAttribute.cs to remove any attempt to initialize SimpleMembership
. It resides in the “Filters” folder of the project. I left the namespace and commented out the entire class with the class attributes.
namespace RadvSampleDaycareApp001.Filters
{
//RAD-001 - Disable Simple Membership
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
//... NOTE: not all code shown for brevity.
// Just comment out the whole class. We will enable it later.
}
}
Step 3 – Modify AccountController.cs under the Controllers folder to remove any attempt to initialize SimpleMembership
.
Step3a – Comment out the [InitializeSimpleMembership]
attribute just above the AccountController
class definition.
namespace RadvSampleDaycareApp001.Controllers
{
[Authorize]
//RAD-001 - disable SimpleMemebership [InitializeSimpleMembership]
public class AccountController : Controller
. . .
Step3c – Comment out the call to WebSecurity.Login
in the Login
function. This is in a if statement. After it is commented out, you will receive an “Unreachable code detected” warning since it will always take the same path. Not to worry as it is only temporary.
public ActionResult Login(LoginModel model, string returnUrl)
{
//RAD-001-TEMP - disable SimpleMemebership
if (ModelState.IsValid && WebSecurity.Login(model.UserName,
model.Password, persistCookie: model.RememberMe))
//RAD-001-TEMP - Added if statement below to keep the block
{
//RAD-002-CHG - Changed the default return to now go
// the the main menu controller on successful login
return RedirectToAction("DisplayMenu", "MainMenu");
}
. . .
Step3d – Disable the calls to WebSecurity
in the [HttpPost] Register
function. I Ended up just returning ASAP so that the code is never reached. This will also cause an “Unreachable code detected” warning.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
//RAD-001-TEMP - The code below returns immediately to disable the calls to WebSecurity
return View(model);
if (ModelState.IsValid)
{
. . .
Points of Interest
I wasn’t ready to setup a database quite yet and I couldn’t get localdb to work quickly for SimpleMembership on the Azure Web Site. It is ok, since it won’t work at scale out anyway, but if you’re struggling just trying to get development started using Azure Web Sites and MVC 4, it could impede your progress. See Section 001 – Disable Simple Membership.
History
- r001v001a – 05/09/2013 – Initial revision.