public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
    {
        var values = new SettingsPropertyValueCollection();

        foreach (SettingsProperty setting in props)
        {
            var value = new SettingsPropertyValue(setting);
            value.IsDirty = false;
            value.SerializedValue = GetValue(setting);
            values.Add(value);
        }
        return values;
    }
    // Will be called when MySettingsClass.GetPreviousVersion(propName) is called
    // This method's job is to retrieve a setting value from the previous version
    // of the settings w/o updating the setting at the storage location
    public SettingsPropertyValue GetPreviousVersion(SettingsContext context, SettingsProperty prop)
    {
        // If there's no previous setting version, return an empty property
        // NOTE: the LFSP returns an empty property for all app-scoped settings, so so do we
        string previousVersion = GetPreviousVersionNumber();
        if (IsApplicationScoped(prop) || string.IsNullOrEmpty(previousVersion))
        {
            // NOTE: can't just return null, as the settings engine turns that into
            // a default property -- have to return a SettingsPropertyValue object
            // with the PropertyValue set to null to really build an empty property
            SettingsPropertyValue propval = new SettingsPropertyValue(prop);
            propval.PropertyValue = null;
            return propval;
        }

        // Get the property value from the previous version
        // NOTE: if it's null, the settings machinery will assume the current default value
        // ideally, we'd want to use the previous version's default value, but a) that's
        // likely to be the current default value and b) if it's not, that data is lost
        return GetPropertyValue(prop, previousVersion);
    }
    public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
    {
        //Create new collection of values
        SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();

        //Iterate through the settings to be retrieved

        foreach (SettingsProperty setting in props)
        {
            SettingsPropertyValue value = new SettingsPropertyValue(setting);
            value.IsDirty = false;
            value.SerializedValue = GetValue(setting);
            values.Add(value);
        }
        return values;
    }
    private void SetValue(SettingsPropertyValue propVal)
    {
        System.Xml.XmlElement MachineNode = null;
        System.Xml.XmlElement SettingNode = null;

        //Determine if the setting is roaming.
        //If roaming then the value is stored as an element under the root
        //Otherwise it is stored under a machine name node
        try
        {
            if (IsRoaming(propVal.Property))
            {
                SettingNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + propVal.Name);
            }
            else
            {
                SettingNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + Environment.MachineName + "/" + propVal.Name);
            }
        }
        catch (Exception ex)
        {
            SettingNode = null;
        }

        //Check to see if the node exists, if so then set its new value
        if ((SettingNode != null))
        {
            SettingNode.InnerText = propVal.SerializedValue.ToString();
        }
        else
        {
            if (IsRoaming(propVal.Property))
            {
                //Store the value as an element of the Settings Root Node
                SettingNode = SettingsXML.CreateElement(propVal.Name);
                SettingNode.InnerText = propVal.SerializedValue.ToString();
                SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(SettingNode);
            }
            else
            {
                //Its machine specific, store as an element of the machine name node,
                //creating a new machine name node if one doesnt exist.
                try
                {
                    MachineNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + Environment.MachineName);
                }
                catch (Exception ex)
                {
                    MachineNode = SettingsXML.CreateElement(Environment.MachineName);
                    SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(MachineNode);
                }

                if (MachineNode == null)
                {
                    MachineNode = SettingsXML.CreateElement(Environment.MachineName);
                    SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(MachineNode);
                }

                SettingNode = SettingsXML.CreateElement(propVal.Name);
                SettingNode.InnerText = propVal.SerializedValue.ToString();
                MachineNode.AppendChild(SettingNode);
            }
        }
    }
Example #5
0
    public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
    {
        string username = (string)context["UserName"];
        bool isAuthenticated = (bool)context["IsAuthenticated"];

        Dictionary<string, object> values = _profileValues.ContainsKey(username) ? _profileValues[username] : null;

        SettingsPropertyValueCollection spvc = new SettingsPropertyValueCollection();

        foreach (SettingsProperty prop in collection)
        {
            SettingsPropertyValue spv = new SettingsPropertyValue(prop);
            if (values != null && values.ContainsKey(prop.Name))
            {
                spv.PropertyValue = values[prop.Name];
            }
            else
            {
                spv.PropertyValue = prop.DefaultValue;
            }
            spvc.Add(spv);
        }
        return spvc;
    }
    private void SetSetting(SettingsPropertyValue setProp)
    {
        // Define the XML path under which we want to write our settings if they do not already exist
        XmlNode SettingNode = null;

        try
        {
            // Search for the specific settings node we want to update.
            // If it exists, return its first child node, (the <value>data here</value> node)
            SettingNode = XMLConfig.SelectSingleNode("//setting[@name='" + setProp.Name + "']").FirstChild;
        }
        catch (Exception)
        {
            SettingNode = null;
        }

        // If we have a pointer to an actual XML node, update the value stored there
        if ((SettingNode != null))
        {
            if (setProp.Property.SerializeAs.ToString() == "String")
            {
                SettingNode.InnerText = setProp.SerializedValue.ToString();
            }
            else
            {
                // Write the object to the config serialized as Xml - we must remove the Xml declaration when writing
                // the value, otherwise .Net's configuration system complains about the additional declaration.
                SettingNode.InnerXml = setProp.SerializedValue.ToString().Replace(@"<?xml version=""1.0"" encoding=""utf-16""?>", "");
            }
        }
        else
        {
            // If the value did not already exist in this settings file, create a new entry for this setting

            // Search for the application settings node (<Appname.Properties.Settings>) and store it.
            XmlNode tmpNode = XMLConfig.SelectSingleNode("//" + APPNODE);

            // Create a new settings node and assign its name as well as how it will be serialized
            XmlElement newSetting = xmlDoc.CreateElement("setting");
            newSetting.SetAttribute("name", setProp.Name);

            if (setProp.Property.SerializeAs.ToString() == "String")
            {
                newSetting.SetAttribute("serializeAs", "String");
            }
            else
            {
                newSetting.SetAttribute("serializeAs", "Xml");
            }

            // Append this node to the application settings node (<Appname.Properties.Settings>)
            tmpNode.AppendChild(newSetting);

            // Create an element under our named settings node, and assign it the value we are trying to save
            XmlElement valueElement = xmlDoc.CreateElement("value");
            if (setProp.Property.SerializeAs.ToString() == "String")
            {
                valueElement.InnerText = setProp.SerializedValue.ToString();
            }
            else
            {
                // Write the object to the config serialized as Xml - we must remove the Xml declaration when writing
                // the value, otherwise .Net's configuration system complains about the additional declaration
                valueElement.InnerXml = setProp.SerializedValue.ToString().Replace(@"<?xml version=""1.0"" encoding=""utf-16""?>", "");
            }

            //Append this new element under the setting node we created above
            newSetting.AppendChild(valueElement);
        }
    }
    // Retrieve settings from the configuration file
    public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext sContext, SettingsPropertyCollection settingsColl)
    {
        // Create a collection of values to return
        SettingsPropertyValueCollection retValues = new SettingsPropertyValueCollection();

        // Create a temporary SettingsPropertyValue to reuse
        SettingsPropertyValue setVal;

        // Loop through the list of settings that the application has requested and add them
        // to our collection of return values.
        foreach (SettingsProperty sProp in settingsColl)
        {
            setVal = new SettingsPropertyValue(sProp);
            setVal.IsDirty = false;
            setVal.SerializedValue = GetSetting(sProp);
            retValues.Add(setVal);
        }
        return retValues;
    }
    void SetValue(SettingsPropertyValue propVal)
    {
        XmlElement SettingNode = null;

        try
        {
            SettingNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + propVal.Name);
        }
        catch 
        {
        }

        if (SettingNode == null)
        {
            SettingNode = SettingsXML.CreateElement(propVal.Name);
            SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(SettingNode);
        }

        SettingNode.InnerText = propVal.SerializedValue == null ? null : propVal.SerializedValue.ToString();
    }
    private void SetValue(SettingsPropertyValue propVal)
    {
        Monitor.Enter(SetLock);

        XmlElement settingNode;

        try
        {
            settingNode = (XmlElement)SettingsXML.SelectSingleNode(SETTINGSROOT + "/" + propVal.Name);
            settingNode.InnerText = propVal.SerializedValue.ToString();
        }
        catch
        {
            settingNode = SettingsXML.CreateElement(propVal.Name);
            settingNode.InnerText = propVal.SerializedValue.ToString();
            SettingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(settingNode);
        }
        finally
        {
            Monitor.Exit(SetLock);
        }
    }
    public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
    {
        Monitor.Enter(GetValuesLock);
        SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();

        //Iterate through the settings to be retrieved
        foreach(SettingsProperty setting in collection)
        {
            SettingsPropertyValue value = new SettingsPropertyValue(setting);

            value.IsDirty = false;
            value.SerializedValue = GetValue(setting);
            values.Add(value);
        }

        Monitor.Exit(GetValuesLock);
        return values;
    }
    public ProfileInfoCollection FindProfilesByPropertyValue(SettingsProperty property, SearchOperator searchOperator, object value)
    {
        // instantiate an empty ProfileInfoCollection to use it for return
        ProfileInfoCollection pic = new ProfileInfoCollection();

        // try and open the connection and get the results
        try
        {
            // get the connection we're going to use
            using (SqlConnection conn = new SqlConnection(_connectionStringName))
            {

                // instantiate a SettingsPropertyValue from the property
                // to use it to serialize the value for comparison with the database
                SettingsPropertyValue spv = new SettingsPropertyValue(property);
                spv.PropertyValue = value;

                // set common parameters of the aspnet_Profile_FindProfiles stored procedure
                SqlCommand FindProfilesCommand = new SqlCommand("aspnet_Profile_FindProfiles", conn);
                FindProfilesCommand.CommandType = CommandType.StoredProcedure;
                FindProfilesCommand.CommandTimeout = _commandTimeout;
                FindProfilesCommand.Parameters.Add("@ApplicationName", System.Data.SqlDbType.NVarChar, 256).Value = base.ApplicationName;
                FindProfilesCommand.Parameters.Add("@PropertyName", System.Data.SqlDbType.NVarChar, 256).Value = property.Name;
                FindProfilesCommand.Parameters.Add("@OperatorType", System.Data.SqlDbType.Int).Value = (Int32)searchOperator;

                // if the serialized property value is of type string
                // carry on if the size is within allowed limits
                Boolean bTooBig = false;
                if (spv.SerializedValue is String)
                {
                    // if the serialized value is bigger than the PropertyValueString column's length
                    if (((String)spv.SerializedValue).Length > Int32.MaxValue)
                    {
                        bTooBig = true;
                    }
                    else
                    {
                        if (searchOperator == SearchOperator.Contains || searchOperator == SearchOperator.FreeText)
                        {
                            // if the searchOperator is a freetext operator then pass the value unaltered
                            FindProfilesCommand.Parameters.Add("@PropertyValueString",
                                System.Data.SqlDbType.NVarChar, Int32.MaxValue).Value = spv.PropertyValue;
                            FindProfilesCommand.Parameters.Add("@PropertyValueBinary",
                                System.Data.SqlDbType.VarBinary, Int32.MaxValue).Value = DBNull.Value;
                        }
                        else
                        {
                            // otherwise serialise the value before passing it
                            FindProfilesCommand.Parameters.Add("@PropertyValueString",
                                System.Data.SqlDbType.NVarChar, Int32.MaxValue).Value = spv.SerializedValue;
                            FindProfilesCommand.Parameters.Add("@PropertyValueBinary",
                                System.Data.SqlDbType.VarBinary, Int32.MaxValue).Value = DBNull.Value;
                        }

                        // set the parameter @PropertyType to indicate the property is a string
                        FindProfilesCommand.Parameters.Add("@PropertyType", System.Data.SqlDbType.Bit).Value = 0;
                    }
                }
                else
                {
                    if (((SqlBinary)spv.SerializedValue).Length > Int32.MaxValue)
                    {
                        bTooBig = true;
                    }
                    else
                    {
                        if (searchOperator == SearchOperator.Contains || searchOperator == SearchOperator.FreeText)
                        {
                            // if the searchOperator is a freetext operator then pass the value unaltered to the
                            // @PropertyValueString because we are passing a string anyway not a binary
                            FindProfilesCommand.Parameters.Add("@PropertyValueString",
                                System.Data.SqlDbType.NVarChar, Int32.MaxValue).Value = spv.PropertyValue;
                            FindProfilesCommand.Parameters.Add("@PropertyValueBinary",
                                System.Data.SqlDbType.VarBinary, Int32.MaxValue).Value = DBNull.Value;
                        }
                        else
                        {
                            // otherwise just serialise the value and pass it to the @PropertyBinaryValue
                            FindProfilesCommand.Parameters.Add("@PropertyValueString",
                                System.Data.SqlDbType.NVarChar, Int32.MaxValue).Value = DBNull.Value;
                            FindProfilesCommand.Parameters.Add("@PropertyValueBinary",
                                System.Data.SqlDbType.VarBinary, Int32.MaxValue).Value = spv.SerializedValue;
                        }

                        // set the parameter @PropertyType to indicate the property is a binary
                        FindProfilesCommand.Parameters.Add("@PropertyType", System.Data.SqlDbType.Bit).Value = 1;
                    }
                }

                if (bTooBig)
                {
                    // if the size is out of limits throw an exception
                    throw new ProviderException("Property value length is too big.");
                }

                // Open the database
                conn.Open();

                // Get a DataReader for the results we need
                using (SqlDataReader rdr = FindProfilesCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (rdr.Read())
                    {
                        // create a ProfileInfo with the data you got back from the current record of the SqlDataReader
                        ProfileInfo pi = new ProfileInfo(rdr.GetString(rdr.GetOrdinal("UserName")),
                            rdr.GetBoolean(rdr.GetOrdinal("IsAnonymous")),
                            DateTime.SpecifyKind(rdr.GetDateTime(rdr.GetOrdinal("LastActivityDate")), DateTimeKind.Utc),
                            DateTime.SpecifyKind(rdr.GetDateTime(rdr.GetOrdinal("LastUpdatedDate")), DateTimeKind.Utc), 0);

                        // add the ProfileInfo you just created to the ProfileInfoCollection that we will return
                        pic.Add(pi);
                    }
                }
            }
        }
        catch (Exception e)
        {
            // if anything went wrong, throw an exception
            throw new ProviderException("An error occured while finding profiles with your search criteria!", e);
        }

        return pic;
    }
    private Boolean GetPropertyValueFromReader(SettingsPropertyValue spv, SqlDataReader rdr)
    {
        try
        {
            // get the ordinals of value columns, just a speed optimization issue
            Int32 PropertyValueStringOrdinal = rdr.GetOrdinal("PropertyValueString");
            Int32 PropertyValueBinaryOrdinal = rdr.GetOrdinal("PropertyValueBinary");

            // Get the value based on the SerializeAs value
            switch (spv.Property.SerializeAs)
            {
                case SettingsSerializeAs.String:
                    {
                        // If the value string is null, set property value to null
                        if (rdr.IsDBNull(PropertyValueStringOrdinal))
                        {
                            spv.PropertyValue = null;
                        }
                        else
                        {
                            // no deserialization needed, so set the PropertyValue the same as the database value, changing its type ofcourse
                            spv.PropertyValue = Convert.ChangeType(rdr.GetString(PropertyValueStringOrdinal), spv.Property.PropertyType);
                        }

                        spv.Deserialized = true;

                        break;
                    }

                case SettingsSerializeAs.Binary:
                    {
                        if (rdr.IsDBNull(PropertyValueBinaryOrdinal))
                        {
                            spv.PropertyValue = null;
                            spv.Deserialized = true;
                        }
                        else
                        {
                            spv.SerializedValue = rdr.GetSqlBinary(PropertyValueBinaryOrdinal).Value;
                            spv.Deserialized = false;
                        }

                        break;
                    }

                case SettingsSerializeAs.Xml:
                    {
                        if (rdr.IsDBNull(PropertyValueStringOrdinal))
                        {
                            spv.PropertyValue = null;
                            spv.Deserialized = true;
                        }
                        else
                        {
                            spv.SerializedValue = rdr.GetString(PropertyValueStringOrdinal);
                            spv.Deserialized = false;
                        }

                        break;
                    }
                default:
                    {
                        throw new ProviderException(
                            String.Format("Could not determine correct serialization format for profile property '{0}'.", spv.Name));
                    }
            }
        }
        catch (Exception e)
        {
            throw new ProviderException(String.Format("Error deserialising profile property '{0}'.", spv.Name), e);
        }

        // set is not dirty, we are just reading
        spv.IsDirty = false;

        return true;
    }
    SettingsPropertyValue GetPropertyValue(SettingsProperty prop, string version)
    {
        SettingsPropertyValue value = new SettingsPropertyValue(prop);

        // Only User-scoped settings can be found in the Registry.
        // By leaving the Application-scoped setting's value at null,
        // we get the "default" value
        if (IsUserScoped(prop))
        {
            using (RegistryKey key = CreateRegKey(prop, version))
            {
                value.SerializedValue = key.GetValue(prop.Name);
            }
        }

        value.IsDirty = false;
        return value;
    }
    public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
    {
        //JH
        string username = (string)context["UserName"];
        bool isAuthenticated = (bool)context["IsAuthenticated"];
        Guid uniqueID = GetUniqueID(username, isAuthenticated, false);

        SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection();

        foreach (SettingsProperty prop in collection)
        {
            SettingsPropertyValue pv = new SettingsPropertyValue(prop);
            svc.Add(pv);
        }

        activity(username, "Set property values", false);
    }
    public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
    {
        //JH
        string username = (string)context["UserName"];

        SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection();

        foreach (SettingsProperty prop in collection)
        {
            SettingsPropertyValue pv = new SettingsPropertyValue(prop);
            svc.Add(pv);
        }

        activity(username, "Got Property Values", false);

        return svc;
    }
 // Methods
 public void Add(SettingsPropertyValue property)
 {
 }