CodeVerge.Net Beta


   Explore    Item Entry    Members      Register  Login  
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML

Free Download:




Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.security Tags:
Item Type: NewsGroup Date Entered: 3/7/2007 3:23:47 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 23 Views: 21 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
24 Items, 2 Pages 1 2 |< << Go >> >|
KarlR
Asp.Net User
Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 3:23:47 PM

0/0

I have created a custom membership provider that is declared in the web.config file like this:

<add name="MyMembershipProvider"

      type="MyMembershipProvider"

      requiresQuestionAndAnswer="True"

      enablePasswordRetrieval="True"

      enablePasswordReset="True" 

      connectionString="Data Source=Karlweb;Initial Catalog=Reports;Persist Security Info=True;User ID=VBUser;Password=Password"

      providername="System.Data.SqlClient"/> 

This works, but I am technically defining my db connection in two places: the ConnectionString property for the whole site (named "ReportsConnectionString" AND this one for the Membership provider.  I want to just call it out in the site connectionstring and reference that in the one for the provider.  I tried this:

<add name="MyMembershipProvider"

      type="MyMembershipProvider"

      requiresQuestionAndAnswer="True"

      enablePasswordRetrieval="True"

      enablePasswordReset="True"

      connectionStringName="ReportsConnectionString"

      providername="System.Data.SqlClient"/>

..but I keep getting a "connectionstring property has not been initialized" error when I attempt to use the custom Membership Provider.  I thought I should be able to just reference an existing connectionstring by using the "connectionStringName" property (?).  Can anyone see where I am going wrong?

 I would greatly appreciate any suggestions.  Thank you!

James_2JS
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 4:37:17 PM

0/0

I know this is for a RoleProvider and not a MembershipProvider, but the principal is the same... this works for me...

<

roleManager enabled="true" defaultProvider="OracleRoleProvider">

<

providers>

<

clear/>

<

add connectionStringName="SpecRefConnection" name="OracleRoleProvider" type="OracleRoleProvider" applicationName="SPEC_REF"/>

</

providers>

</

roleManager>

Perhaps it's the ProviderName causing an issue??

KarlR
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 6:20:57 PM

0/0

James, thanks for your reply.  I probably should have posted my entire MembershipProvider tag:

<membership defaultProvider="MyMembershipProvider">

  <providers>

<add name="MyMembershipProvider"

      type="MyMembershipProvider"

      requiresQuestionAndAnswer="True"

      enablePasswordRetrieval="True"

      enablePasswordReset="True" 

      connectionString="Data Source=Karlweb;Initial Catalog=Reports;Persist Security Info=True;User ID=VBUser;Password=Password"

      providername="System.Data.SqlClient"/> 

  </providers> 

I think I got the ProviderName correct, but I am still experiencing this error.  Is there another related setting that I need to change in order to use this property?

Thanks again!

justenough
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 6:28:44 PM

0/0

Can you post the <connectionStrings> section in your config file?

 
James_2JS
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 6:30:57 PM

0/0

Sorry... I wasn't clear there... I don't think you need the providername, as it will pick this up from the connectionstring...

So let's see what your connection strings section looks like!!

KarlR
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 6:44:25 PM

0/0

You were right - I should have also posted my ConnectionString setting (whups).  Here it is: 

<connectionStrings>

  <add name="Project_Management.My.MySettings.WebReportsConnectionString"

      connectionString="Data Source=Karlweb;Initial Catalog=Reports;Persist Security Info=True;User ID=VBUser;Password=Password"

      providerName="System.Data.SqlClient" />

</connectionStrings>

And for the record, I have tried both "WebReportsConnectionString" and "Project_Management.My.Settings.WebReportsConnectionString" & got the same error.  As you can see, the connectionstring is identical, so it would be ideal to work this out so I don't have to change it in two places.

Again, thank you for your quick replies!

justenough
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 7:07:28 PM

0/0

To me it seems the problem is the peirods in the name.  When you tried it with only WebReportsConnectionString, are you sure you had the spelling and everything correct? 

KarlR
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 7:18:42 PM

0/0

Yup - I copy & pasted it just to make sure.  Even the capitalization is the same.

Sorry - I don't know what I am doing wrong.  Thanks again.

justenough
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 8:22:37 PM

0/0

can you tell me the exact error message that you're getting
KarlR
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 8:56:16 PM

0/0

It's still the same as my original post: "connectionstring property has not been initialized".  This only happens when I trigger something that uses the membership provider (like logging in, for example).

Thanks again.

justenough
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 9:11:53 PM

0/0

Try removing the following line from the Membership Provider section; 

providerName

="System.Data.SqlClient"
KarlR
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 9:32:43 PM

0/0

Unfortunately, I still get the same error.  I tried a couple of different versions of the connectionStringName value as well, without success.

I appreciate your continued suggestions.  I will keep experimenting, but please let me know if you think of anything else.

justenough
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/7/2007 9:41:41 PM

0/0

I assume that because this is a custom provider, you have a class called MyMembershipProvider.  Can you show me the code in that class that reads the connection string out of the config file?
James_2JS
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/8/2007 12:10:51 AM

0/0

I would change this to be name="WebReportsConnectionString" - forget about everything else... and then use WebReportsConnectionString in you MembershipProvider definition... and ditch the ProviderName entry there too!!

KarlR
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/8/2007 12:15:10 PM

0/0

You are right - I do have a class called MyMembershipProvider.  When I use my original web config settings (where I call out the the duplicate connection string setting), it works great.  I am just trying to have the MembershipProvider use a reference to my <connectionstrings> section.  Here is what the MyMembershipProvider.vb file looks like:

Imports Microsoft.VisualBasic

Imports System.Data

Imports System.Data.SqlClient

Imports Project_Management.Functions 'This was required to access the e-mail function in this class (?)

 

Public Class MyMembershipProvider

    Inherits MembershipProvider

 

    '---for database access use---

    Private connStr As String 'Connection string (populated on Initialize)

 

    Private _requiresQuestionAndAnswer As Boolean

    Private _minRequiredPasswordLength As Integer

 

    Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)

 

        '===retrives the attribute values set in

        'web.config and assign to local variables===

 

        If config("requiresQuestionAndAnswer").ToUpper = "TRUE" Then _

            _requiresQuestionAndAnswer = True

 

        connStr = config("connectionString")

        MyBase.Initialize(name, config)

    End Sub

James, I did try what you suggested, but I still got the same error.  Could you use the examples in my earlier post to show me exactly what you mean?  Thank you!

James_2JS
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/8/2007 12:33:19 PM

0/0

Sorry... I've been a bit slow over the last couple of days... jetlagged I think!!! Smile

Anyway... I can see what the problem is now... and should've picked it up sooner...

Your Initialize code is only handling having a connectionString... and doesn't deal with a connectionStringName!! So here's what you need to do... add some conditional code into your initialize method... one branch of which can pull out the connection string as you've done above... and one branch that can pull it out of the settings as follows:

Dim ConnectionStringSettings As ConnectionStringSettings = _
          ConfigurationManager.ConnectionStrings(config("connectionStringName"))

        If ConnectionStringSettings Is Nothing OrElse ConnectionStringSettings.ConnectionString.Trim() = "" Then
            Throw New ProviderException("Connection string cannot be blank.")
End If connStr = ConnectionStringSettings.ConnectionString
 Hope this helps!!
KarlR
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/8/2007 1:18:08 PM

0/0

PLEASE do not appologize!  I greatly appreciate your continued perserverence (on my problem).

Thanks to your help, I got it (I think).  For some reason, your above code kept returning "Nothing" for "ConnectionStringSettings" as I stepped through the code.  I kept poking around in the ConfigurationManager, and got this code to work:

    Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)

 

        '===retrives the attribute values set in

        'web.config and assign to local variables===

 

        If config("requiresQuestionAndAnswer").ToUpper = "TRUE" Then _

            _requiresQuestionAndAnswer = True

 

        connStr = ConfigurationManager.ConnectionStrings.Item(1).ConnectionString

 

        If connStr Is Nothing OrElse connStr = "" Then

            Throw New Provider.ProviderException("Connection string cannot be blank.")

        End If

 

        MyBase.Initialize(name, config)

 

    End Sub

I don't like having that hard-coded "1" in there, but that's where the setting resides in my site.  Just for reference, this is what my web.config section looks like:

<membership defaultProvider="MyMembershipProvider">

  <providers>                   

    <add name="MyMembershipProvider"

          type="MyMembershipProvider"

          requiresQuestionAndAnswer="True"

          enablePasswordRetrieval="True"

          enablePasswordReset="True" 

          connectionStringName="WebReportsConnectionString"

          />   

  </providers>

</membership>

Again, THANK YOU very much for your continued help!  I will mark this as "answered", but please let me know if you see anywhere I can further optimize the above (like removing the "1").

James_2JS
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/8/2007 1:43:41 PM

0/0

No worries!!

OK... you're right... the 1 is ugly... and isn't generic... and needs to be replaced!! You've also moved away from allowing a connection string in the membership provider... you are insisting the a connectionStringName is supplied... while this might be great for you... it's not as good for re-use!! So here's what I would do....

' try and get the connection string setting
connStr = config("connectionString")
If String.IsNullOrEmpty(connStr) Then
    ' no connection string given... so try the connection string name
    Dim ConnectionStringSettings As ConnectionStringSettings = _
            ConfigurationManager.ConnectionStrings(config("connectionStringName"))
    ' make sure we've got a connection string setting that corresponds to the name
    If ConnectionStringSettings IsNot Nothing Then
        ' pull out the connection string from the settings
        connStr = ConnectionStringSettings.ConnectionString
    Else
        Throw New
ProviderException("Connection string name doesn't exist.")
    End If
End If

' make sure we have got a connection string after all of that
If String.IsNullOrEmpty(connStr) Then
    ' we haven't so through an exception
    Throw New ProviderException("
Connection string cannot be blank.")
End If

So that's what you should do... but you also need help working out why you've had to use "1" in the first place... this should be a simple case of matching the connectionStringName entry on the membership provider with the name in the connection strings. You can test this by evaluating

ConfigurationManager.ConnectionStrings(1).Name

This is what should be in the membership provider settings!

justenough
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/8/2007 1:44:41 PM

0/0

Have you tried changing this line:

'connStr = config("connectionString")

to

connStr = config("connectionStringName")

justenough
Asp.Net User
Re: Unable to use connectionStringName in a Custom Membership Provider definition3/8/2007 1:53:38 PM

0/0

KarIR,

I don't think you have to make the changes you have.  I think your only problem was that you were referencing the wrong key.  I think all you have to do is put your code back to the way it was and then just change the key name to connectionStringName. 

24 Items, 2 Pages 1 2 |< << Go >> >|


Free Download:


Web:
Unable to use connectionStringName in a Custom Membership Provider ... Unable to use connectionStringName in a Custom Membership Provider definition. Last post 03-08-2007 10:56 AM by James_2JS. 23 replies. ...
Unable to retrieve all user information with custom membership ... My web.config contains multiple membership Providers, because in the future I would like to use the custom membership provider to control ...
RE: Multiple Membership providers and AddUserToRole Membership and Profile are two completely separate sections in the ... but custom profile and role providers. ... This is where I get an error: Unable to ...
Custom Provider-Based Services Configuring Custom Provider-Based Services. In order to use a provider-based .... Similar properties may be found in the Membership class and in other ASP. ...
CodeProject: Custom MembershipProvider and RoleProvider ... Custom MembershipProvider and RoleProvider Implementations that use Web Services ...... SqlMembershipProvider" conne