3

I tried changing it via

protected void RemoveRoles(User user)
    {
        using (new SecurityDisabler())
        {
            UserRoles.FromUser(user).RemoveAll();
            UserProfile profile = user.Profile;
            profile.SetPropertyValue("IsAdministrator", "False");
            profile.Save();
        }
    }

and

protected void RemoveRoles(User user)
    {
        using (new SecurityDisabler())
        {
            UserRoles.FromUser(user).RemoveAll();
            UserProfile profile = user.Profile;
            profile.IsAdministrator = false;
            profile.Save();
        }
    }

However, both functions throw an exception: "This property cannot be set for anonymous users." on line where I try to set IsAdministrator property.

The user object has correct values when I debug the code and I'm able to remove all the roles added via rolemanager. However, I'm unable to remove admin role from the user.

5

Here is the correct code:

var user = Sitecore.Security.Accounts.User.FromName("sitecore\\test", true);
RemoveRoles(user);

protected void RemoveRoles(Sitecore.Security.Accounts.User user)
{
    Sitecore.Security.Accounts.UserRoles.FromUser(user).RemoveAll();
    Sitecore.Security.UserProfile profile = user.Profile;
    profile.IsAdministrator = false;
    profile.Save();
}

The important thing is to pass true as the second parameter in the FromName method for the isAuthenticated parameter. Thanks to that you will be able to set IsAdministrator to false.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.