private static void ConfigureMembershipReboot(IAppBuilder app)
        {
            var cookieOptions = new CookieAuthenticationOptions
            {
                AuthenticationType = MembershipRebootOwinConstants.AuthenticationType
            };
            Func<IDictionary<string, object>, UserAccountService> uaFunc = env =>
            {
                var appInfo = new OwinApplicationInformation(
                    env,
                    "Test",
                    "Test Email Signature",
                    "/Login",
                    "/Register/Confirm/",
                    "/Register/Cancel/",
                    "/PasswordReset/Confirm/");

                var config = new MembershipRebootConfiguration();
                var emailFormatter = new EmailMessageFormatter(appInfo);
                // uncomment if you want email notifications -- also update smtp settings in web.config
                config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));

                var svc = new UserAccountService(config, new DefaultUserAccountRepository());
                svc.TwoFactorAuthenticationPolicy = new OwinCookieBasedTwoFactorAuthPolicy(env);
                return svc;
            };
            Func<IDictionary<string, object>, AuthenticationService> authFunc = env =>
            {
                return new OwinAuthenticationService(cookieOptions.AuthenticationType, uaFunc(env), env);
            };

            app.UseMembershipReboot(cookieOptions, uaFunc, authFunc);
        }
        public async Task Invoke(IDictionary<string, object> env)
        {
            var ctx = new OwinContext(env);

            using (var db = new BrockAllen.MembershipReboot.Ef.DefaultUserAccountRepository())
            {
                var settings = SecuritySettings.FromConfiguration();
                var mrConfig = new MembershipRebootConfiguration(settings);
                mrConfig.ConfigureCookieBasedTwoFactorAuthPolicy(new OwinCookieBasedTwoFactorAuthPolicy(env));

                var appInfo = new OwinApplicationInformation(env,
                    "Test", "Test Email Signature",
                    "UserAccount/Login",
                    "UserAccount/Register/Confirm/",
                    "UserAccount/Register/Cancel/",
                    "UserAccount/PasswordReset/Confirm/",
                    "UserAccount/ChangeEmail/Confirm/");

                var emailFormatter = new EmailMessageFormatter(appInfo);
                if (settings.RequireAccountVerification)
                {
                    // uncomment if you want email notifications -- also update smtp settings in web.config
                    //config.AddEventHandler(new EmailAccountCreatedEventHandler(emailFormatter));
                }
                // uncomment if you want email notifications -- also update smtp settings in web.config
                //config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));

                var uaSvc = new UserAccountService(mrConfig, db);
                var anSvc = new OwinAuthenticationService(uaSvc, env);
                try
                {
                    ctx.SetUserAccountService(uaSvc);
                    ctx.SetAuthenticationService(anSvc);
                    
                    await next(env);
                }
                finally
                {
                    ctx.SetUserAccountService(null);
                    ctx.SetAuthenticationService(null);
                }
            }
        }
        private static void ConfigureMembershipReboot(IAppBuilder app)
        {
            System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<DefaultMembershipRebootDatabase, BrockAllen.MembershipReboot.Ef.Migrations.Configuration>());
            var cookieOptions = new CookieAuthenticationOptions
            {
                AuthenticationType = MembershipRebootOwinConstants.AuthenticationType
            };

            var appInfo = new OwinApplicationInformation(
                app,
                "Test",
                "Test Email Signature",
                "/Login",
                "/Register/Confirm/",
                "/Register/Cancel/",
                "/PasswordReset/Confirm/");

            var config = new MembershipRebootConfiguration();
            var emailFormatter = new EmailMessageFormatter(appInfo);
            // uncomment if you want email notifications -- also update smtp settings in web.config
            config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));

            Func<IDictionary<string, object>, UserAccountService> uaFunc = env =>
            {
                var svc = new UserAccountService(config, new DefaultUserAccountRepository());
                var debugging = false;
#if DEBUG
                debugging = true;
#endif
                svc.ConfigureTwoFactorAuthenticationCookies(env, debugging);
                return svc;
            };
            Func<IDictionary<string, object>, AuthenticationService> authFunc = env =>
            {
                return new OwinAuthenticationService(cookieOptions.AuthenticationType, uaFunc(env), env);
            };

            app.UseMembershipReboot(cookieOptions, uaFunc, authFunc);
        }
        private static MembershipRebootConfiguration CreateMembershipRebootConfiguration(IAppBuilder app)
        {
            var config = new MembershipRebootConfiguration();
            config.RequireAccountVerification = false;
            config.AddEventHandler(new DebuggerEventHandler());

            var appInfo = new OwinApplicationInformation(
                app,
                "Test",
                "Test Email Signature",
                "/UserAccount/Login",
                "/UserAccount/ChangeEmail/Confirm/",
                "/UserAccount/Register/Cancel/",
                "/UserAccount/PasswordReset/Confirm/");

            var emailFormatter = new EmailMessageFormatter(appInfo);
            // uncomment if you want email notifications -- also update smtp settings in web.config
            config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));
            // uncomment to enable SMS notifications -- also update TwilloSmsEventHandler class below
            //config.AddEventHandler(new TwilloSmsEventHandler(appinfo));

            // uncomment to ensure proper password complexity
            //config.ConfigurePasswordComplexity();
            
            return config;
        }
        private static MembershipRebootConfiguration CreateMembershipRebootConfiguration(IAppBuilder app)
        {
            var appInfo = new OwinApplicationInformation(
                app,
                "Test",
                "Test Email Signature",
                "/Login",
                "/Register/Confirm/",
                "/Register/Cancel/",
                "/PasswordReset/Confirm/");

            var config = new MembershipRebootConfiguration();
            var emailFormatter = new EmailMessageFormatter(appInfo);
            // uncomment if you want email notifications -- also update smtp settings in web.config
            config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));
            return config;
        }
        private static MembershipRebootConfiguration CreateMembershipRebootConfiguration(IAppBuilder app)
        {
            var config = new MembershipRebootConfiguration();
            config.EmailIsUsername = true;
            config.RequireAccountVerification = true;
            config.AddEventHandler(new DebuggerEventHandler());

            var appInfo = new OwinApplicationInformation(
                app: app,
                appName: "Test",
                emailSig: "Test Email Signature",
                relativeLoginUrl: "/useraccount/login",
                relativeConfirmChangeEmailUrl: "/useraccount/changeemail/confirm/",
                relativeCancelNewAccountUrl: "/useraccount/register/cancel/",
                relativeConfirmPasswordResetUrl: "/useraccount/passwordreset/confirm/");

            var emailFormatter = new EmailMessageFormatter(appInfo);
            // uncomment if you want email notifications -- also update smtp settings in web.config
            config.AddEventHandler(new EmailAccountEventsHandler(emailFormatter));
            // uncomment to enable SMS notifications -- also update TwilloSmsEventHandler class below
            //config.AddEventHandler(new TwilloSmsEventHandler(appinfo));

            // uncomment to ensure proper password complexity
            //config.ConfigurePasswordComplexity();

            return config;
        }