Late, late answer, but you can use reflection:
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);
}
}
Then in your code you can do something like this:
MyMembershipProvider provider = new MyMembershipProvider();
NameValueCollection config = new NameValueCollection();
config["username"] = "myUsername";
config["password"] = "myPassword";
provider.Initialize("MyProvider", config);
provider.AddTo(Membership.Providers);
This is a hack because we use reflection to set the private field "_ReadOnly", but it works.
:
http://elegantcode.com/2008/04/17/testing-a-membership-provider/
:
http://www.endswithsaurus.com/2010/03/inserting-membershipprovider-into.html
_ReadOnly , , .
,
-Doug