BlogEngine.Core.Providers.XmlRoleProvider.Initialize C# (CSharp) Method

Initialize() public method

Initializes the provider.
/// The name of the provider is null. /// /// The name of the provider has a length of zero. /// /// An attempt is made to call on a provider after the provider has already been initialized. ///
public Initialize ( string name, NameValueCollection config ) : void
name string /// The friendly name of the provider. ///
config System.Collections.Specialized.NameValueCollection /// A collection of the name/value pairs representing the provider-specific attributes specified in the configuration for this provider. ///
return void
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (string.IsNullOrEmpty(name))
            {
                name = "XmlMembershipProvider";
            }

            if (string.IsNullOrEmpty(config["description"]))
            {
                config.Remove("description");
                config.Add("description", "XML role provider");
            }

            base.Initialize(name, config);

            // Initialize _XmlFileName and make sure the path
            // is app-relative
            var filename = config["xmlFileName"];

            if (string.IsNullOrEmpty(filename))
            {
                filename = "roles.xml";
            }
            else
            {
                if (!VirtualPathUtility.IsAppRelative(filename))
                {
                    throw new ArgumentException("xmlFileName must be app-relative");
                }
            }

            this.xmlFileName = filename;
            config.Remove("xmlFileName");

            // Make sure we have permission to read the XML data source and
            // throw an exception if we don't
            var permission = new FileIOPermission(FileIOPermissionAccess.Write, this.XmlFullyQualifiedPath);
            permission.Demand();

            this.ReadMembershipDataStore();
            if (!File.Exists(this.XmlFullyQualifiedPath))
            {
                this.AddUsersToRoles(this.userNames.ToArray(), this.defaultRolesToAdd);
            }

            // Now that we know a xml file exists we can call it.
            this.ReadRoleDataStore();

            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                var attr = config.GetKey(0);
                if (!string.IsNullOrEmpty(attr))
                {
                    throw new ProviderException(string.Format("Unrecognized attribute: {0}", attr));
                }
            }
        }