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?
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.
If you were to merge the InitializeSimpleMembershipAttribute
into the Global.asax.cs
Application_Start
so that the SimpleMembershipProvider
would be initialized without any AccountController
routes being called...
...it could look something like this: http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html
// The using below is needed for "UsersContext" - it will be relative to your project namespace using MvcApplication1.Models; using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Threading; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using WebMatrix.WebData; namespace MvcApplication1 { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); // Ensure ASP.NET Simple Membership is initialized only once per app start LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); } private static SimpleMembershipInitializer _initializer; private static object _initializerLock = new object(); private static bool _isInitialized; private class SimpleMembershipInitializer { public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With