Software Add Membership Provider

I have a .Net application that uses an arbitrary number of membership providers. I will not go into the reasons, but I do not want them to be pre-configured, but I want to create and add them programmatically. Is there any way to do this? I have no problem creating providers, but Membership.Providers is read-only, so I cannot add them.

+3
source share
3 answers

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();
// Configure your provider here.  For example,
config["username"] = "myUsername";
config["password"] = "myPassword";
provider.Initialize("MyProvider", config); 

// Add your provider to the membership provider list
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

+6

- ( ) web.config. , .

, .

+1

, , .

, ( ), ( - , - ).

:

- .

- , , .

(), , web.config, .

- , , web.config , , .

( - "", ) exe, app.config, ( ). DPAPI .

0

Source: https://habr.com/ru/post/1717822/


All Articles