23

Everytime I restart Debugging with Visual Studio I get this freaking error:

You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.

But that is everytime, and when i deploy the app to prod. I get this error here and then, randomly.

I do put the proper tag :

[Authorize(Roles = "admin")]
[InitializeSimpleMembership]
public class IndexController : Controller

to controller and here is the filter the way it is. It just doesnt wanna work.

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Threading;
using System.Web.Mvc;
using MeetingTaskManagement.Models;
using WebMatrix.WebData;

namespace MeetingTaskManagement.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)
        {
            // Ensure ASP.NET Simple Membership is initialized only once per app start
            LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
        }

        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);
                }
            }
        }
    }
}

can someone help me sort this out?

DarthVader
  • 52,984
  • 76
  • 209
  • 300

6 Answers6

67

Remove the SimpleMemberShip attribute from your controllers and trash it. Add the following to your global.asax.

    private static SimpleMembershipInitializer _initializer;
    private static object _initializerLock = new object();
    private static bool _isInitialized;

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

        LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
    }

    public class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            using (var context = new UsersContext())
                context.UserProfiles.Find(1);

            if (!WebSecurity.Initialized)
                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
        }
    }
Bastaspast
  • 1,022
  • 8
  • 9
  • 11
    I like the part when @Bastaspast said "trash it". – jingtao Oct 01 '13 at 16:27
  • What should the line 'context.UserProfiles.Find(1);' do? It appears to return a list of users when I examine it in debug mode. But this line results in an error asking me to update the db with code first migration, but if I remove that line it appears to work fine. Am I safe to remove that line? – mal Apr 14 '14 at 08:27
  • 1
    The line is only intended to trigger the creation of the database through codefirst. When you are sure your database always exists, you can safely remove the line. – Bastaspast Apr 18 '14 at 22:36
  • I love this solution but what if we update to ASP.NET MVC 5? Would trashing stuff help :p – Tassisto Apr 23 '14 at 13:03
  • I updated my project to MVC5 last December and encountered no issues at all. Kind Regards. – Bastaspast Apr 24 '14 at 10:53
  • There is the same error when using `Area` in Application :( How to solve? – Amirhossein Mehrvarzi Nov 09 '14 at 21:14
10

To simplify your Global.asax, you can put the code for initialization into class AuthConfig like this:

public static class AuthConfig
{
    private static SimpleMembershipInitializer _initializer;
    private static object _initializerLock = new object();
    private static bool _isInitialized;

    public static void RegisterAuth()
    {
        // Ensure ASP.NET Simple Membership is initialized only once per app start
        LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);

        // To let users of this site log in using their accounts from other sites such as Microsoft, Facebook, and Twitter,
        // you must update this site. For more information visit http://go.microsoft.com/fwlink/?LinkID=252166

        //OAuthWebSecurity.RegisterMicrosoftClient(
        //    clientId: "",
        //    clientSecret: "");

        //OAuthWebSecurity.RegisterTwitterClient(
        //    consumerKey: "",
        //    consumerSecret: "");

        //OAuthWebSecurity.RegisterFacebookClient(
        //    appId: "",
        //    appSecret: "");

        //OAuthWebSecurity.RegisterGoogleClient();
    }

    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);
            }
        }
    }
}
Bolt Thunder
  • 745
  • 6
  • 26
  • This worked great! I just want to add that I had to add a few references to System.Data.Entity, System.Data.Entity.Infrastructure, System.Threading, and WebMatrix.WebData. – ourmandave Aug 31 '15 at 01:30
4

I just switch the [InitializeSimpleMembership] to HomeController, and erase it from AccountController. Because the initializations are made in a lazy way.. I just want to rush it.. and it worked!!!

lienysd
  • 149
  • 1
  • 6
1

I was being a smartass by using a single context both for Membership provider and for my domain models. It turns out that was the problem; you can't use Membership provider and domain classes in a single context.

You need to have two contexts.

DarthVader
  • 52,984
  • 76
  • 209
  • 300
0

The requirement is only to call the WebSecurity Database Connection, you can do the following which is working:

var WebSecDBContx = new UsersContext();
var CreatedByUser = WebSecDBContx.UserProfiles.Find(UserID);
string CreatedByUserName = CreatedByUser.UserName;
raju1208
  • 95
  • 7
0

Why not just create the _AppStart.cshtml in the project root and place the initialization code in there as the error message states.

@{WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("DefaultConnection", "TableName", "ColumnId", "ColumnName", autoCreateTables: false);}