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>
</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
{
[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)
{
}
}
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]
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)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName,
model.Password, persistCookie: model.RememberMe))
{
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)
{
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.
Enterprise ISV leader and chief architect of enterprise software systems. Original architect and developer of the TriZetto Facets software system for the Healthcare Payor marketplace. 20+ years experience in all aspects of software development and hosting services. My passion is in developing rock-solid software patterns and applying them to the ever changing technologies to provide solutions that last. Foundations, Frameworks, Code-Generation, Architecture Placement and selecting the right technology are all criticle to developing systems that stand the test of time.