Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Tip/Trick

Disable MVC 4 SimpleMembership for early development on Azure websites

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 May 2013CPOL2 min read 12K  
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

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.

C#
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.

C#
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.

C#
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.

C#
[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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer RadventureSoft LLC
United States United States
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.

Comments and Discussions

 
-- There are no messages in this forum --