UmbracoLinqPadDriver icon indicating copy to clipboard operation
UmbracoLinqPadDriver copied to clipboard

Membership provider error when saving content

Open deanebarker opened this issue 7 years ago • 4 comments
trafficstars

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);
	}
}

deanebarker avatar Feb 22 '18 22:02 deanebarker

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 :)

Shazwazza avatar Feb 27 '18 02:02 Shazwazza

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!

Shazwazza avatar Jun 22 '18 05:06 Shazwazza

Great package @Shazwazza!!

Any update on this issue?

Sam7 avatar Jan 22 '19 23:01 Sam7

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

Shazwazza avatar Mar 04 '19 13:03 Shazwazza