Link to home
Start Free TrialLog in
Avatar of alivemedia
alivemedia

asked on

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")
        Else
            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.







Avatar of S31B1
S31B1

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

ASKER

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">
            <providers>
                <add name="AliveMediaMembershipProvider" type="AliveMediaMembershipProvider" connectionStringName="mdspotsConnectionString" membershipTable="Administrator" username="Username" password="Password" setAdditionalCookies="True" cookieList="ID, EmpID, FirstName, LastName, Email" selectFields="Username, Password"/>
            </providers>
        </membership>
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?
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 :(
ASKER CERTIFIED SOLUTION
Avatar of alivemedia
alivemedia

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of modus_operandi
Closed, 500 points refunded.
modus_operandi
Community Support Moderator