views:

28

answers:

0

I am attempting to write a Custom Profile Provider for use in a multi-site CMS.

In order to make the CMS more generic I was trying to allow you to specify the Profile that you wish to use on a site by site basis.

The profile to use is stored in the database with the namepace and class name. When a user access something that requires the profile to be accessed it uses a custom profile provider.

In the GetPropertyValues method of the Custom Provider the following code is run.

    string username = (string)context["UserName"];
    bool isAuthenticated = (bool)context["IsAuthenticated"];

    // The serializeAs attribute is ignored in this provider implementation.
    User user = DataRepository.UserProvider.GetByUserName(username, _applicationName)[0];
    SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection();

    if (CmsContext.Current.CurrentSite.XmlParameters.Contains(CmsConstants.UserProfileParameter))
    {
        // Load the Dynamic Class type using the Assembly Namespace, Get its type and Get its SettingPropertyCollection.
        PropertyInfo propInfo = ProfileUtilities.GetCurrentSiteProfile().GetType().GetProperty("Properties" , BindingFlags.Static);
        ppc = (SettingsPropertyCollection)propInfo.GetValue(null, null);
    }

    foreach (SettingsProperty prop in ppc)
    {
        SettingsPropertyValue pv = new SettingsPropertyValue(prop);

        switch (prop.Name)
        {
            case "Forename":
                pv.PropertyValue = user.Forename;
                break;
            case "Surname":
                pv.PropertyValue = user.Surname;
                break;
            default:
                pv.PropertyValue = user.XmlParameters[prop.Name];
                break;
        }

        svc.Add(pv);
    }

    UpdateActivityDates(username, isAuthenticated, true);

    return svc;

This run correctly an returns the SettingsPropertyValueCollection with the correct property names in it.

However when I try to access a Key that should be present in the Profile object that is selected using late binding using:

      profile["Title"] = profileEnum.Current.Value;

Where Title is a property on the profile clase. An exception is thrown.

System.Configuration.SettingsPropertyNotFoundException: The settings property 'Title' was not found.

If I try to access a property that is in the Profile class specified in the web.config this is accessible and no exception occurs.

Also the System.Configuration.SettingBase class contains the wrong number of keys in the hash table.

Is there anyway to do this, I was trying to avoid using a one size fits all field on a base profile.