UmbracoLinqPadDriver
UmbracoLinqPadDriver copied to clipboard
Membership provider error when saving content
When I tried to save content, I ran into an error:
No membership provider found with name UsersMembershipProvider
(Weirdly, I was able to delete content just fine. I only had a problem when saving it.)
The problem is that you can't manually add a provider because the collection is read-only. They're added from the web.config in the web context, I think. I tried a bunch of stuff, but ended up solving this with a horrible, horrible hack -- I reflect the read-only property and manually add a provider.
(Warning: this is bad. But it worked. Also, it was stolen from Stack Overflow...)
// MASSIVE HACK: this manually adds a membership provider in the worst way possible... :-0
var provider = new Umbraco.Web.Security.Providers.UsersMembershipProvider();
provider.Initialize("UsersMembershipProvider", new NameValueCollection());
provider.AddTo(Membership.Providers);
public static class ProviderUtil
{
static private FieldInfo providerCollectionReadOnlyField;
static ProviderUtil()
{
Type t = typeof(ProviderCollection);
providerCollectionReadOnlyField = t.GetField("_ReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
}
static public void AddTo(this ProviderBase provider, ProviderCollection pc)
{
bool prevValue = (bool)providerCollectionReadOnlyField.GetValue(pc);
if (prevValue)
providerCollectionReadOnlyField.SetValue(pc, false);
pc.Add(provider);
if (prevValue)
providerCollectionReadOnlyField.SetValue(pc, true);
}
}
Very interesting :) well the problem with this project and old legacy Umbraco bits is that a lot of this type of hacking is required! Over the years I've tried to update the Umbraco codebase to allow for this to be bootstrapped in a less hacky way, looks like you've discovered another case.
I'll see if i can figure something out when i have time, in the meantime, keep hacking the planet :)
I've just come up against this too, i think it didn't happen in earlier versions of Umbraco since it might not have been referenced but now it is. Need to find some time to get a new version out!
Great package @Shazwazza!!
Any update on this issue?
Oh my inbox backlog is far too big! I'm trying to get through all of the starred items and will try to get to this as soon as possible - almost have some breathing room this month :P