How do I call Initialize on a custom MembershipProvider?

前端 未结 8 694
死守一世寂寞
死守一世寂寞 2020-12-08 08:17

I have read through all the related questions, but I still unable to get the right solution for some reason, something is not right on my side, but not sure what\'s causing

相关标签:
8条回答
  • 2020-12-08 08:57

    Custom Membership provider is initialized automatically and it is not intended to do so manually.

    In my implementation, there is the Initialize metod like below:

    public override void Initialize(string name, NameValueCollection config)
    {
        if (config == null)
            throw new ArgumentNullException("config");
    
    
        // Initialize the abstract base class.
        base.Initialize(name, config);
    }
    

    Keep in mind, that the base.Initialize method is in ProviderBase class which has the following exceptions defined:

    Exceptions:

    • System.ArgumentNullException: The name of the provider is null.
    • System.ArgumentException: The name of the provider has a length of zero.

    • System.InvalidOperationException: An attempt is made to call System.Configuration.Provider.ProviderBase.Initialize(System.String,System.Collections.Specialized.NameValueCollection) on a provider after the provider has already been initialized.

    Isn't the last exception the one you get?

    0 讨论(0)
  • 2020-12-08 09:00

    this forces the initialize to be called

    private readonly Provider _provider;
    
    public AccountMembershipService(Provider provider)
    {
       _provider = provider ?? (Provider) Membership.Provider;
    }
    
    0 讨论(0)
  • 2020-12-08 09:07

    It's true your Initialize method should be called automatically so long as your provider is configured correctly (as it seems to be in your code sample).

    You'll need to clarify how you 'called it manually', and where you tried to cast NameValueCollection. Did it happen inside Initialize?

    Perhaps you should show us your Initialize method (you didn't forget the override keyword, did you? ;-)

    Edit: Well, the Initialize method seems fine too.

    Keep in mind: Membership is a static class, and it loads and initializes the configured providers in a lazy manner. So the construction of your provider, and the call to its Initialize method, will not occur until a call is made to either the Membership.Provider or the Membership.Providers property. Most of the other static methods (such as GetUser()) will do this, but the conclusion is that your Initialize method will not be called until the Membership API is actually used.

    Have you done this, either explicitly or by using a Login control or suchlike?

    0 讨论(0)
  • 2020-12-08 09:13

    Basically the flow goes like this,

    Membership class (a static class) calls and uses MembershipProvider (an abstract class derived from ProviderBase) which SqlMembershipProvider implements (in your case MyMemberShipProvider), thus you gave the your implementation of the data accessing code to your data source in MyMemberShipProvider but you don't call the initialize yourself.

    The Initialize() is virtual method on ProviderBase, when you create your MyMemberShipProvider you override it like below

    class MyMemberShipProvider : MembershipProvider
    {
        private string _connectionStringName;
    
        public override void Initialize(string name, NameValueCollection config)
        {
           // see the config parameter passed in is of type NameValueCollection 
           // it gives you the chance to get the properties in your web.config
           // for example, one of the properties is connectionStringName
    
            if (config["connectionStringName"] == null)
            {
                config["connectionStringName"] = "ApplicationServices";
            }
            _connectionStringName = config["connectionStringName"];
            config.Remove("connectionStringName");          
        }
    }
    

    Without see your code, when you say have an exception that has to do with NameValueCollection, it reminds me of this method above.

    Hope this helps, Ray.

    0 讨论(0)
  • 2020-12-08 09:13

    I had a problem some time ago with this Initialize() method, I will post it here it may be helpful for someone.

    Let's imagine you have the implementation of your custom provider on:

    MyEnterprise.MyArea.MyProviders.CustomProvider
    

    And what you want is use the method GetUserNameByEmail which is inside the provider implementation. There are two ways to call this method, through:

    MyEnterprise.MyArea.MyProviders.CustomProvider.GetUserNameByEmail(email)
    

    Which will not fire the Initialize method as you are calling it by yourself, on the other hand if your call is:

    Membership.GetUserNameByEmail(email)
    

    The Initialize method will be called if necessary, I assume this is on the base constructor or something (didn't dig more).

    Hope this helps. - E.

    0 讨论(0)
  • 2020-12-08 09:14

    For Initialize() to be called, you will need to instantiate your custom membership provider a certain way. Like so:

    MyCustomMembershipProvider myProvider = (MyCustomMembershipProvider)Membership.Providers["NameOfMembershipProviderInConfig"];
    

    Now when you use myProvider, Initialize() from your custom provider will be called.

    0 讨论(0)
提交回复
热议问题