Srdjan Donevski
Srdjan Donevski

Reputation: 71

Sitecore multiple custom user profiles

Is it possible to have more than one custom user profile and if it is how to set up web config file and how to manage custom profiles for two website under the same sitecore instance (same VS solution)?

We had one custom user profile and new requirement came about new website under the same sitecore instance but with the new custom user for the second website. During development of second website we created second custom user profile and everything went fine, we change "inherits" attribute of system.web/profile node in the web.config file to point to second custom use profile and during development it was OK.

The problem now is that only one user profile can log in to the webistes: if we set inherits attribute to "Namespace.Website.NamespaceA.CustomProfileA, Namespace.Website" only profileA will be able to log in to their domain and if we set it to "Namespace.Website.NamespaceB.CustomProfileB, Namespace.Website" only profileB will be able to login to its domain because the switcher will use this one.

All articles in the web describe how to set custom user profile, switcher and switchingProviders for just one custom user profile but there are no examples for my case.

Thanks, Srdjan

Upvotes: 3

Views: 271

Answers (1)

Ben Golden
Ben Golden

Reputation: 1580

Unfortunately, there does not seem to be a clean way to have multiple user profile classes created for you by the API. Typically, you will get the user profile via Sitecore.Context.User.Profile. The Context class is static and the methods that initialize the Profile property are private, so there's nowhere to insert your extra logic.

You could, however, create wrapper classes for the Profile. Start with a base class like this:

public abstract class CustomProfileBase
{
    public CustomProfileBase(Sitecore.Security.UserProfile innerProfile)
    {
        Assert.ArgumentNotNull(innerProfile, nameof(innerProfile));
        InnerProfile = innerProfile;
    }

    public Sitecore.Security.UserProfile InnerProfile { get; protected set; }

    public virtual string GetCustomProperty(string propertyName)
    {
        return InnerProfile.GetCustomProperty(propertyName);
    }

    public virtual void SetCustomProperty(string propertyName, string value)
    {
        InnerProfile.SetCustomProperty(propertyName, value);
    }

    public virtual void Save()
    {
        InnerProfile.Save();
    }

    public virtual string Email
    {
        get { return InnerProfile.Email; }
        set { InnerProfile.Email = value; }
    }

    // Other members omitted for brevity
}

This CustomProfileBase class would have a member that wraps each of the public members of Sitecore.Security.UserProfile. Then, you would create your site specific profile like this:

public class SiteOneProfile : CustomProfileBase
{
    public SiteOneProfile(UserProfile innerProfile) : base(innerProfile)
    {
    }

    public string CustomPropertyOne
    {
        get { return GetCustomProperty("CustomPropertyOne"); }
        set { SetCustomProperty("CustomPropertyOne", value); }
    }
}

Then you would use it from a controller or elsewhere like so:

var profile = new SiteOneProfile(Sitecore.Context.User.Profile);
model.property = profile.CustomPropertyOne;

Update

When using this approach, you would just leave the inherits attribute in the config with its default value. Also, the profile should not have an effect on the ability to login. If you are still having issues with that, please update your question with details of the error you get when logging in.

Upvotes: 2

Related Questions