• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2474
  • Last Modified:

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.







0
alivemedia
Asked:
alivemedia
1 Solution
 
S31B1Commented:
Instead of ConfigurationManager.GetSection("system.web.membership.providers") could you try ConfigurationManager.GetSection("system.web/membership/providers")?
0
 
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">
            <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>
0
 
S31B1Commented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
S31B1Commented:
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?
0
 
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 :(
0
 
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
0
 
modus_operandiCommented:
Closed, 500 points refunded.
modus_operandi
Community Support Moderator
0
 
braalewiCommented:
I found this which looks appears to work so far as I have implemented it.
http://www.lazyasscoder.com/Article.aspx?id=69&title=How+To%3a+Manually+initialize+a+custom+membership+provider
 
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now