Custom role provider not working

T

Tom Van den Brandt

Hi all!

I'm trying to implement a custom roleprovider in asp.net. The new
roleprovider works fine when I access it programmaticaly. However, it
doesn't seem to work with the standard controls. More specifically, I
have a loginview with rolegroups and the parts of the site that should
be viewable for a certain role, do not show up. Did I forget something?

Regards,

Tom
 
G

Guest

Do you have this set up properly in your web.config entries? Do you have a
"remove name=XXx" at the top of the section to remove the default roles
provider before you add your entry for your custom one?
Peter
 
T

Tom Van den Brandt

Yes, I do...

Here's a section from my web.config:


<roleManager defaultProvider="CustomRoleProvider"
enabled="true"
<providers>
<clear />
<remove name="AspNetSqlRoleProvider"/>
<add
name="CustomRoleProvider"
type="CustomRoleProvider"
connectionString="server=rakels10.synstar.be;database=websupporttables;user
id=xxxxx password=xxxxx;"
applicationName="/"
writeExceptionsToEventLog="false" />
</providers>
</roleManager>


I don't get it, I have a testpage where I try some of the methods from
my custom role provider and this works... e.g.:

Dim provider As RoleProvider = New CustomRoleProvider
Dim roles() As String = provider.FindUsersInRole("user", "%")
...


Peter Bromberg [C# MVP] schreef:
 
G

Guest

Then I'd suggest running this in debug mode with the source code project for
your role provider in the solution. Set breakpoints at the appropriate places
and trace through your code to find out what may be wrong.

If your breakpoints don't get hit, that means your configuration setup is
wrong.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




Tom Van den Brandt said:
Yes, I do...

Here's a section from my web.config:


<roleManager defaultProvider="CustomRoleProvider"
enabled="true"<providers>
<clear />
<remove name="AspNetSqlRoleProvider"/>
<add
name="CustomRoleProvider"
type="CustomRoleProvider"
connectionString="server=rakels10.synstar.be;database=websupporttables;user
id=xxxxx password=xxxxx;"
applicationName="/"
writeExceptionsToEventLog="false" />
</providers>
</roleManager>


I don't get it, I have a testpage where I try some of the methods from
my custom role provider and this works... e.g.:

Dim provider As RoleProvider = New CustomRoleProvider
Dim roles() As String = provider.FindUsersInRole("user", "%")
...


Peter Bromberg [C# MVP] schreef:
Do you have this set up properly in your web.config entries? Do you have a
"remove name=XXx" at the top of the section to remove the default roles
provider before you add your entry for your custom one?
Peter
 
T

Tom Van den Brandt

Thanks for the helping hand...

I've tried you're suggestion. I've put some breakpoints in the role
provider code and they do get hit. So no configuration error I guess...
It looks to me that the role provider is working fine, but the roles are
not added to the user data or something (because the content defined in
rolegroups is not shown)...


Peter Bromberg [C# MVP] schreef:
 
T

Tom Van den Brandt

Some extra info:

When I programatically 'use' the roleprovider everything works fine, e.g.:

Dim provider As RoleProvider = New CustomRoleProvider
Dim roles() As String = provider.GetRolesForUser("Tom")
Dim roleexists As Boolean = provider.IsUserInRole("Tom", "user")

This all returns the correct results.

But when I use:
System.Web.Security.Roles.IsUserInRole("Tom", "user")

I get incorrect results.

I guess some binding or something needs to be done after authentication.
I've found an article on putting some extra code in a global asax file
but that doesn't seem to work either. This is the code I've tried:

Protected Sub Application_AuthenticateRequest(ByVal sender As Object,
ByVal e As System.EventArgs)
Try
If Request.IsAuthenticated Then
Dim roleprovider As RoleProvider = New CustomRoleProvider
Dim roles As String() =
roleprovider.GetRolesForUser(User.Identity.Name)
HttpContext.Current.User = New
System.Security.Principal.GenericPrincipal(User.Identity, roles)
End If
Catch ex As Exception
Console.Write(ex.ToString)
End Try
End Sub


Peter Bromberg [C# MVP] schreef:
 
T

Tom Van den Brandt

Problem solved...

While debugging I saw that my role names had trailing spaces in them (in
the database) and because 'user' <> 'user ' the role groups were not
working. How this spaces got in the db, no idea... I've added a trim
function to remove unnecessary spaces and everything works fine now...

Peter Bromberg [C# MVP] schreef:
 
G

Guest

See that? Knew you could do it! :)
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




Tom Van den Brandt said:
Problem solved...

While debugging I saw that my role names had trailing spaces in them (in
the database) and because 'user' <> 'user ' the role groups were not
working. How this spaces got in the db, no idea... I've added a trim
function to remove unnecessary spaces and everything works fine now...

Peter Bromberg [C# MVP] schreef:
Then I'd suggest running this in debug mode with the source code project for
your role provider in the solution. Set breakpoints at the appropriate places
and trace through your code to find out what may be wrong.

If your breakpoints don't get hit, that means your configuration setup is
wrong.
Peter
 
Joined
Jun 11, 2007
Messages
1
Reaction score
0
Tom,
I too have to implement my own RoleProvider. Can you help me get started. I'm not sure where I put the part to create the RoleClass, and how its 'wired up' to the LoginControls.

My project/webiste is currently working using the Default Database Schema with the SQLMemebership and SQLRoleProviders, but due to scope change I need to change the datasource of my RoleTables.

For Starters I made my own class called: clsMyRoleProvider.vb, and copied the code I found here (http://msdn2.microsoft.com/en-us/library/317sza4k(vs.80).aspx)

From your above sample I'll modify my Webconfig.

Question: Do you have them sign in? and get the username: strUser= me.lblUser.txt?

then 'somehow' make a trip to the server to get the role out from the Database.

When you make the trip to the DB and return a role value (ie Admin, Author, Browser) how do you connect or inform the LoginView Control what to render?

As I said before, I've it working with the SQLRole Providers, so I'm not sure if I need to set some properties on the control to look at MyRoleProvider.

All help is appreciated.
Thanks
rwiethorn
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top