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

Posted on 2007-10-15
Last Modified: 2013-11-07
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.

Question by:alivemedia
    LVL 4

    Expert Comment

    Instead of ConfigurationManager.GetSection("system.web.membership.providers") could you try ConfigurationManager.GetSection("system.web/membership/providers")?
    LVL 2

    Author Comment

    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"/>
    LVL 4

    Expert Comment

    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.
    LVL 4

    Expert Comment

    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?
    LVL 2

    Author Comment

    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 :(
    LVL 2

    Accepted Solution

    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
    LVL 1

    Expert Comment

    Closed, 500 points refunded.
    Community Support Moderator

    Expert Comment

    I found this which looks appears to work so far as I have implemented it.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now