How do I call Initialize on a custom MembershipProvider?

前端 未结 8 754
死守一世寂寞
死守一世寂寞 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 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.

提交回复
热议问题