Custom Profile Provider Implementation

K

Kirk

I have successfully, implemented a custom Membership Provider to a SQL
2000 table, however, I am having problems doing the same with a
Profile Provider.

As I understand it, the steps for both of these are similar: Create a
new class (MyMembershipProvider and MyProfileProvider). Add the
"Inherits..." value to the provider (MembershipProvider and
ProfileProvider). Modify the web.config file to assign this class and
it's connection string. (Feel free to correct me if the above is
incorrect).

I have done this for my Profile Provider, and I can't seem to "wire
up" the new class to the application. My class starts like this:

Public Class MyProfileProvider
Inherits ProfileProvider

When I type the above inherits statement, all of the methods for this
class show up, which leads me to believe this part worked. I then
modified my web.config file like this:

<profile enabled="true" defaultProvider="MyProfileProvider" >
<providers>
<add name="MyProfileProvider" type="MyProfileProvider"
connectionStringName="Data Source=myweb;Initial
Catalog=WebReport;Persist Security Info=True;User
ID=VBUser;Password=VBUser" providername="System.Data.SqlClient" />
</providers>
<properties>
<add name="Country" type="string"/>
<add name="Gender" type="string"/>
<add name="Age" type="Int32"/>
</properties>
</profile>

In theory, I should be able to type "Profile.Country = ..." in VB and
get intellisense, right? When I type this, it tells me that "Country"
is not a member of "Profile"?

Am I missing an Imports statement or something else minor? I would
GREATLY appreciate any suggestions or comments. Thank you!
 
G

Guest

Howdy,

I'm not sure if you can use custom attibutes, plus everything must
compileable in order to use intelli sense with Profile property. Anyway i
prepared a working example so you can figure out what's wrong with your code.

-- mycustomprovider.vb (in app_code folder)
Imports Microsoft.VisualBasic
Imports System.Web.Profile
Imports System.Configuration

Public Class MyProfileProvider
Inherits ProfileProvider

Public Overrides Property ApplicationName() As String
Get
Return Nothing
End Get
Set(ByVal value As String)
End Set
End Property

Public Property ConnectionStringName() As String
Get
Return Nothing
End Get
Set(ByVal value As String)

End Set
End Property

Public Overrides Function DeleteInactiveProfiles(ByVal authenticationOption
As System.Web.Profile.ProfileAuthenticationOption, ByVal
userInactiveSinceDate As Date) As Integer

End Function

Public Overloads Overrides Function DeleteProfiles(ByVal usernames() As
String) As Integer

End Function

Public Overloads Overrides Function DeleteProfiles(ByVal profiles As
System.Web.Profile.ProfileInfoCollection) As Integer

End Function

Public Overrides Function FindInactiveProfilesByUserName(ByVal
authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal
usernameToMatch As String, ByVal userInactiveSinceDate As Date, ByVal
pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As
Integer) As System.Web.Profile.ProfileInfoCollection
Return Nothing
End Function

Public Overrides Function FindProfilesByUserName(ByVal authenticationOption
As System.Web.Profile.ProfileAuthenticationOption, ByVal usernameToMatch As
String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef
totalRecords As Integer) As System.Web.Profile.ProfileInfoCollection
Return Nothing
End Function

Public Overrides Function GetAllInactiveProfiles(ByVal authenticationOption
As System.Web.Profile.ProfileAuthenticationOption, ByVal
userInactiveSinceDate As Date, ByVal pageIndex As Integer, ByVal pageSize As
Integer, ByRef totalRecords As Integer) As
System.Web.Profile.ProfileInfoCollection
Return Nothing
End Function

Public Overrides Function GetAllProfiles(ByVal authenticationOption As
System.Web.Profile.ProfileAuthenticationOption, ByVal pageIndex As Integer,
ByVal pageSize As Integer, ByRef totalRecords As Integer) As
System.Web.Profile.ProfileInfoCollection
Return Nothing
End Function

Public Overrides Function GetNumberOfInactiveProfiles(ByVal
authenticationOption As System.Web.Profile.ProfileAuthenticationOption, ByVal
userInactiveSinceDate As Date) As Integer

End Function

Public Overrides Function GetPropertyValues(ByVal context As
System.Configuration.SettingsContext, ByVal collection As
System.Configuration.SettingsPropertyCollection) As
System.Configuration.SettingsPropertyValueCollection

Dim result As New System.Configuration.SettingsPropertyValueCollection()

For Each settings As System.Configuration.SettingsProperty In collection
result.Add(New SettingsPropertyValue(settings))
Next

Return result

End Function

Public Overrides Sub SetPropertyValues(ByVal context As
System.Configuration.SettingsContext, ByVal collection As
System.Configuration.SettingsPropertyValueCollection)

End Sub
End Class

-- end mycustomprovider.vb --

-- web config --



<profile defaultProvider="MyProfileProvider" enabled="true">
<providers>
<add name="MyProfileProvider" type="MyProfileProvider"/>
</providers>
<properties>
<add name="Country" type="String"/>
</properties>
</profile>

-- end web.config --

Hope this helps
Milosz
 
K

Kirk

First off, thank you both for your quick responses!

Sloan - the example you gave me works (thank you), but only for a Web
SITE. I should have mentioned that I am developing a ASP.NET Web
APPLICATION. When I followed the example on web site, I correctly got
the intellisense for the property - but not when I did the exact same
changes on a web application. Any thoughts?

Milosz - your example compiled correctly, but I still can't reference
the properties of the profile from my code. That is, when I type
"Profile.", "Country" is not one of the selectable properties. If I
type it anyway, I get a "'Country' is not a member of 'Profile'
error". This is the same error I am currently experiencing. Does
this also have something to do with me developing a web Application
(vs web Site)? If this works (in a web application) for you, would
you be willling to e-mail it to me? Maybe I am missing something
stupid. I would appreciate any more help you could give me!

Thanks in advance!
 
G

Guest

Hi Kirk,

Any chances your web application uses old compilation model? Was it migrated
from asp.net 1.1 to 2.0? Where do you use it? on the page code or elswhere? I
think you're trying to access profile from outside page scope. Is that the
case?
 
K

Kirk

Milosz,

I don't think my web application uses the old model. To test your
example, I created a brand new web application project and then added
the code above. Is there a project setting that I need to verifiy or
change?

I am trying to access the profile information in the VB code for a
page, like "Default.aspx.vb". Is there a reference I need to add to
allow the page to access the profile information?

Thank you again for your reply & help. I look forward to your
response.
 
G

Guest

I just noticed:

"...the example you gave me works (thank you), but only for a Web
SITE. I should have mentioned that I am developing a ASP.NET Web
APPLICATION. When I followed the example on web site, I correctly got
the intellisense for the property - but not when I did the exact same
changes on a web application. Any thoughts?..."

Could you exmplain what you mean?
 
K

Kirk

Milosz,

When I mentioned that this did not work in a Web Application, I meant
that while the code would compile, when I tried to reference a profile
property I got a "xxx is not a member of Profile" error. This also
caused the intellisense for "Profile." to not function properly.

However, I have been making some progress using the above post by
Sloan. A link at the bottom of the page he references points to here:

http://www.gotdotnet.com/workspaces/workspace.aspx?id=406eefba-2dd9-4d80-a48c-b4f135df4127

This is an add-in for VS 2005 to allow you to overcome some Web
Application limitations. Once I installed this, I was able to
reference the properties of the profile without problems. I add this
to the web page [VB]:

Private ReadOnly Property Profile() As WebProfile
Get
Return New WebProfile(Context.Profile)
End Get
End Property

....and I can now access the properties of my Profile. I had to write
some more code in a separate class to retrive the data from my SQL
database, but that seems to work well enough. However, I still have
another problem. As this definition is defined in the page, the
properties do not "carry over" to other pages and are "reset" when the
page loads. I want to call this declaration out in a global class so
that the Profile information persists accross the entire site, but I
don't know how to do this. The "Context" property is only available
for web pages - not VB classes, and I can't seem to declare it
differently there and make it work. Does anyone have more experience
in using this add-in?

Again, I greatly appreciate your feedback & help.
 
G

Guest

Howdy,

Now i see what you meant. You're using old compilation model, i asked you
this question in my second reply :)
 
K

Kirk

Milosz,

Pardon my ignorance, but what is the difference between the "old" and
the "new" compilation model? Is this something I can upgrade or
simply change? Is this part of VS 2005 SP1 (which I have not yet
installed)?

I have made only minor progress in getting the profile properties to
persist across pages. I changed my code to this:

Private ReadOnly Property Profile() As WebProfile
Get
If Session("CurrentProfile") IsNot Nothing Then

Return Session("CurrentProfile")
Else

Dim prof = New WebProfile(Context.Profile)
Session("CurrentProfile") = prof
End If
'Return New WebProfile(Context.Profile)
End Get
End Property

As you can see, I am trying to store this whole class in a session
varaible. It seems to work, but I still have to add the above to
every page - which I was trying to avoid. I still can't add this to a
global VB class, because I get a "Reference to a non-shared member
requires an object reference." error for the "Session" member.

Thank you again for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top