Custom Membership Provider: Calling Initialize problems - which config section?

I am using a custom membership provider and I need to be able to validate against 3 different tables so I wrote a helper function that updates the variables set during the initialize sub.

Problem is when I try to validate a user not using a login control but my own I need to know how to initilize the membership provider, specifically how to pass the proper web config section.  So I figure I would create a New Sub that called the initialize:

My Page Code Behind for the login page:
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLogin.Click
        Dim provider As New AliveMediaMembershipProvider
        Dim loginSuccess As Boolean = provider.ValidateUserByType(txtEmail.Text, txtUserPassword.Text, "Physicians", "Id, FirstName, LastName, Email, UserPassword", "Email", "UserPassword", "Id")
        If loginSuccess Then Response.Redirect("~/physicians/Physician.aspx")
    End Sub

My New Sub inside my mebership provider that calls initialize:
    Public Sub New()
        Dim config As Collections.Specialized.NameValueCollection = ConfigurationManager.GetSection("system.web.membership.providers")
        Initialize("AliveMediaMembershipProvider", config)
    End Sub

Inherited Initilize Sub  that gets called:
   Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)
        MyBase.Initialize(name, config)

        If config("connectionStringName") Is Nothing OrElse config("connectionStringName").Trim() = "" Then
            Throw New ArgumentException("Connection string is not configured in web.config, add connectionStringName")
            membershipTable = config("membershipTable")
        End If
// more code
End Sub

I get an Object Reference not set error on this I believe because I an not passing in the proper value for the config.

Who is Participating?
alivemediaAuthor Commented:
I got it:
    Public Sub New()
        Dim config As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/web.config")
        Dim configSection As System.Web.Configuration.MembershipSection = CType(config.GetSection("system.web/membership"), System.Web.Configuration.MembershipSection)
        Dim myConfig As Collections.Specialized.NameValueCollection = configSection.Providers("AliveMediaMembershipProvider").Parameters
        Initialize("AliveMediaMembershipProvider", myConfig)
    End Sub

I had to first load the web.config, then get the section, then get the provider I wanted.  Intellisense / Object Browser FTW
Instead of ConfigurationManager.GetSection("system.web.membership.providers") could you try ConfigurationManager.GetSection("system.web/membership/providers")?
alivemediaAuthor Commented:
I still get this error when doing that:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 25:         MyBase.Initialize(name, config)
Line 26:
Line 27:         If config("connectionStringName") Is Nothing OrElse config("connectionStringName").Trim() = "" Then
Line 28:             Throw New ArgumentException("Connection string is not configured in web.config, add connectionStringName")
Line 29:         Else

Here is the portion of my web.config if that helps:
<membership defaultProvider="AliveMediaMembershipProvider">
                <add name="AliveMediaMembershipProvider" type="AliveMediaMembershipProvider" connectionStringName="mdspotsConnectionString" membershipTable="Administrator" username="Username" password="Password" setAdditionalCookies="True" cookieList="ID, EmpID, FirstName, LastName, Email" selectFields="Username, Password"/>
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

I'm only going on what I would do if I was trying to access an attribute in a normal XML file. But you could try prefixing the any attributes you are trying to retrieve with an @ symbol.
scrap that last answer, it's probably rubbish, have you set a breakpoint on your function to see what values are contained within the collection?
alivemediaAuthor Commented:
It's returning nothing, I even tried just doing a GetScetion("system.web") and still get nothing in the collection.  I am assuming GetSection only works of user defined sections of the web.config. At a stand still here :(
Closed, 500 points refunded.
Community Support Moderator
I found this which looks appears to work so far as I have implemented it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.