Reading membership section from web.config

前端 未结 3 1172
再見小時候
再見小時候 2021-01-12 23:52

I have created a custom MembershipProvider class, so far so good, however, I am not sure how to read the configuration settings from the web.config file.

I tried to

相关标签:
3条回答
  • 2021-01-13 00:35

    If you have overridden the System.Web.Security.MembershipProvider in your own class you can get the web.config membership settings as Robban suggests, just by calling the System.Web.Security.Membership methods. However, these calls will be directed to your membership provider class, so you will need to provide some implementation.

    Supposing you have overridden the MembershipProvider class and added a section in the config file, as in the original question above. A call to int i = Membership.MinRequiredPasswordLength will be directed to YOUR implementation. This might look like this:

       public override int MinRequiredPasswordLength
        {
            get { return _minRequiredPasswordLength; }
        }
    

    MSDN gives a complete example here. The example shows you how to read the config file to set the local properties like _minRequiredPasswordLength.

    0 讨论(0)
  • 2021-01-13 00:38

    As this is set as your default provider it should be enough to:

    int i = Membership.MinRequiredPasswordLength;
    

    And that would return an int specifying the minimum required password length.

    0 讨论(0)
  • 2021-01-13 00:39

    here is the solution in code :

            MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
            string defaultProvider = membershipSection.DefaultProvider;
            ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];
            string connectionStringName = providerSettings.Parameters["connectionStringName"];
            string connectionUsername = providerSettings.Parameters["connectionUsername"];
            string connectionPassword = providerSettings.Parameters["connectionPassword"];
            string connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
    
    0 讨论(0)
提交回复
热议问题