Question

I have MVC 4 application with out of the box Simple Membership mechanism implemented. Recently I have discovered that every single time I call any of the controllers in my application OnActionExecuting method in InitializeSimpleMembershipAttribute filter is executed! Even though I have commented out InitializeSimpleMembership attribute on Account controller like so:

[Authorize]
//[InitializeSimpleMembership]
public class AccountController : Controller
{

the filter is still getting called! Where is this call coming from?

Was it helpful?

Solution

I would usually delete the InitializeSimpleMembership file which is in the Filters folder by default. You want it to initialize once at start up so the best place for that is Global.asax

Now to keep the structure of the application_start() method consistent in Global.asax, I would add it to a config file being called by that method. You could create a new one but I would usually just add it to AuthConfig. AuthConfig has the default OAuth code, so it makes sense to keep all the authentication methods together.

The Method in called RegisterAuth is called by global.asax once at start up. Just add

//Local Authentication
            WebSecurity.InitializeDatabaseConnection("MyDbConnection", "UserProfile", "UserId", "UserName", autoCreateTables: false);

You will have a few errors on build which are resolved by removing using statements referencing the filters folder (maybe only if you have resharper) and removing the InitializeSimpleMembership attribute (it is no longer needed).

In my example I have autoCreateTables to false as I use a Database project in my solution. If you use another method such as code-first you may want that to true

OTHER TIPS

You should check the _AppStart.cshtml file if there is any. Or search the code that intialize the membership. It might look like this:

if (!WebSecurity.Initialized)
{
    WebSecurity.InitializeDatabaseConnection("UserConnectionString", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top