Can't get custom ASP.NET MembershipProvider to initialize

109 Views Asked by At

I created a custom MembershipProvider in my ASP.NET Web Application using code I found on MSDN which overrides the "stock" MembershipProvider, since I can't use the aspnet_regsql utility due to database issues with my hosting company. The following snippet is from the Initialize function:

        public override void Initialize ( string name, NameValueCollection config ) {
            if ( config == null )
                throw new ArgumentNullException ( "config" );
             if ( name == null || name.Length == 0 )
                name = "SiteUserProvider";
             if ( String.IsNullOrEmpty ( config ["description"] ) ) {
                config.Remove ( "description" );
                config.Add ( "description", "Pueblo411.com Custom Membership provider" );
            }

            base.Initialize ( name, config );
            pApplicationName = GetConfigValue ( config ["applicationName"], HostingEnvironment.ApplicationVirtualPath );
            pMaxInvalidPasswordAttempts = Convert.ToInt32 ( GetConfigValue ( config ["maxInvalidPasswordAttempts"], "5" ) );
            pPasswordAttemptWindow = Convert.ToInt32 ( GetConfigValue ( config ["passwordAttemptWindow"], "10" ) );
            pMinRequiredNonAlphanumericCharacters = Convert.ToInt32 ( GetConfigValue ( config ["minRequiredNonAlphanumericCharacters"], "1" ) );
            pMinRequiredPasswordLength = Convert.ToInt32 ( GetConfigValue ( config ["minRequiredPasswordLength"], "7" ) );
            pPasswordStrengthRegularExpression = Convert.ToString ( GetConfigValue ( config ["passwordStrengthRegularExpression"], "" ) );
            pEnablePasswordReset = Convert.ToBoolean ( GetConfigValue ( config ["enablePasswordReset"], "true" ) );
            pEnablePasswordRetrieval = Convert.ToBoolean ( GetConfigValue ( config ["enablePasswordRetrieval"], "true" ) );
            pRequiresQuestionAndAnswer = Convert.ToBoolean ( GetConfigValue ( config ["requiresQuestionAndAnswer"], "false" ) );
            pRequiresUniqueEmail = Convert.ToBoolean ( GetConfigValue ( config ["requiresUniqueEmail"], "true" ) );
            connectionString = GetConfigValue ( config ["connectionStringName"], "p411" );
            string temp_format = config["passwordFormat"];
            ConnectionStringSettings ConnectionStringSettings =ConfigurationManager.ConnectionStrings[config["connectionStringName"]];
            if ( ConnectionStringSettings == null || ConnectionStringSettings.ConnectionString.Trim ( ) == "" ) 
                throw new ProviderException ( "Connection string cannot be blank." );
            connectionString = ConnectionStringSettings.ConnectionString;
            Configuration cfg =WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
            machineKey = ( MachineKeySection ) cfg.GetSection ( "system.web/machineKey" );
            if ( machineKey.ValidationKey.Contains ( "AutoGenerate" ) )
                if ( PasswordFormat != MembershipPasswordFormat.Clear )
                    throw new ProviderException ( "Hashed or Encrypted passwords are not supported with auto-generated keys." );
        }

Everything compiles just fine, but when I try to call CreateUser, I get an error saying that the connection string hasn't been initialized. I tested to see if the connection string variable was being populated, but it turns out that the Initialize function isn't even firing. Here's how I'm using the custom provider in my page to call CreateUser

                var provider = new SiteUserProvider();
                MembershipUser newUser = provider.CreateUser(uname.Value, psw.Value,email.Value,null,null,false,Guid.NewGuid(), out MembershipCreateStatus status);

I don't understand why the Initialize method isn't being fired. Any ideas?

0

There are 0 best solutions below