Custom role provider not working

Discussion in 'ASP .Net' started by Tom Van den Brandt, May 31, 2007.

  1. 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
     
    Tom Van den Brandt, May 31, 2007
    #1
    1. Advertising

  2. 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

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




    "Tom Van den Brandt" wrote:

    > 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
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, May 31, 2007
    #2
    1. Advertising

  3. 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
    >
     
    Tom Van den Brandt, Jun 4, 2007
    #3
  4. 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" wrote:

    > 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
    > >

    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jun 4, 2007
    #4
  5. 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:
    > 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
    >
     
    Tom Van den Brandt, Jun 5, 2007
    #5
  6. 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:
    > 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
    >
     
    Tom Van den Brandt, Jun 5, 2007
    #6
  7. 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
    >
     
    Tom Van den Brandt, Jun 5, 2007
    #7
  8. 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" wrote:

    > 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
    > >

    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jun 5, 2007
    #8
  9. Tom Van den Brandt

    rwiethorn

    Joined:
    Jun 11, 2007
    Messages:
    1
    Location:
    Cincinnati
    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
     
    rwiethorn, Jun 11, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVuIFIu?=

    debug a custom membership and role provider?

    =?Utf-8?B?QmVuIFIu?=, Mar 27, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    3,854
    =?Utf-8?B?dWxyaWNoIHNjaHVtYWNoZXI=?=
    May 17, 2006
  2. CK

    Custom Role Provider

    CK, Jul 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    5,077
    Winista
    Jul 25, 2006
  3. Alias
    Replies:
    5
    Views:
    681
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    Feb 18, 2007
  4. sloan
    Replies:
    1
    Views:
    514
    Chad Scharf
    Jul 3, 2007
  5. =?Utf-8?B?QXJuZQ==?=

    Authorization with custom role provider

    =?Utf-8?B?QXJuZQ==?=, Aug 17, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    459
    =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?=
    Aug 17, 2007
Loading...

Share This Page