Why InitializeSimpleMembershipAttribute in MVC 4 app

CallMeLaNN picture CallMeLaNN · Oct 23, 2012 · Viewed 19.4k times · Source

I think my understanding on SimpleMembershipProvider is almost 60% and the rest is getting to know how it internally work.

You can quickly found some issue when using [InitializeSimpleMembership] filter only in AccountController (the default template). I think anywhere you use Memberhsip API or WebMatrix.WebSecurity, you need to make sure this filter should be called first.

Later, If you use User.IsInRole in my _Layout.cshtml. You need to apply the filter to all controllers, then you start registering it in globally.

However I just realize there is LazyInitializer.EnsureInitialized which make the initialization performed only once per app start.

So why the SimpleMembershipInitializer (in the filter) is not directly in Application_Start? Is there any reason to use the filter?

Answer

Matt Magpayo picture Matt Magpayo · Nov 2, 2012

I believe the template used an attribute for database initialization so that the non-authenticated portions of the site would still work if the initialization failed.

For most practical purposes, it's best to just have this done in the App_Start.