예제 #1
0
        private static void CreateMembershipProviderTables()
        {
            AuthenticationServiceInitializer.InitializeDatabaseConnection(autoCreateTables: true);

            // Force lazy database initialization.
            int nonexistentUserInt = WebSecurity.GetUserId(Guid.NewGuid().ToString());

            if (nonexistentUserInt != -1)
            {
                throw new ApplicationException("Unexpected GetUserId result.");
            }
        }
예제 #2
0
        private static void SetUpAdminAccount()
        {
            CheckElevatedPrivileges();

            AuthenticationServiceInitializer.InitializeDatabaseConnection(autoCreateTables: true);

            const string adminUserName = AuthenticationDatabaseInitializer.AdminUserName;

            int id = WebSecurity.GetUserId(adminUserName);

            if (id == -1)
            {
                throw new ApplicationException("Missing '" + adminUserName + "' user entry in Common.Principal entity. Please execute DeployPackages.exe, with AspNetFormsAuth package included, to initialize the 'admin' user entry.");
            }

            string adminPassword = InputPassword();

            try
            {
                WebSecurity.CreateAccount(adminUserName, adminPassword);
                Console.WriteLine("Password successfully initialized.");
            }
            catch (MembershipCreateUserException ex)
            {
                if (ex.Message != "The username is already in use.")
                {
                    throw;
                }

                var token   = WebSecurity.GeneratePasswordResetToken(adminUserName);
                var changed = WebSecurity.ResetPassword(token, adminPassword);
                if (!changed)
                {
                    throw new ApplicationException("Cannot change password. WebSecurity.ResetPassword failed.");
                }

                Console.WriteLine("Password successfully changed.");
            }
        }