System.Web.Security.Roles.GetRolesForUser() is not returning the correct results

Discussion in 'ASP .Net Security' started by Nathan Sokalski, Aug 9, 2008.

  1. When I call System.Web.Security.Roles.GetRolesForUser() it returns no
    results (an array of length 0) even though I have roles associated with the
    currently logged in user. I am able to get the username by calling
    System.Web.Security.Membership.GetUser().UserName. Why am I unable to get
    the Roles, and what could I need to change? The <membership> and
    <roleManager> elements in my Web.config are as follows:

    <membership defaultProvider="CustomizedProvider">
    <providers>
    <add name="CustomizedProvider"
    type="System.Web.Security.SqlMembershipProvider"
    connectionStringName="logindb"
    applicationName="Membership"
    minRequiredPasswordLength="8"
    minRequiredNonalphanumericCharacters="0"
    requiresQuestionAndAnswer="false"
    requiresUniqueEmail="false"/>
    </providers>
    </membership>

    <roleManager enabled="true" defaultProvider="CustomizedRoleProvider">
    <providers>
    <add name="CustomizedRoleProvider"
    type="System.Web.Security.SqlRoleProvider"
    connectionStringName="logindb"
    applicationName="Membership"/>
    </providers>
    </roleManager>

    Everything that I have tried with Membership has given me no trouble, but
    Roles seems to want to pretend that there are no roles, even though I have
    added the roles using the ASP.NET Website Administration Tool, and even
    looked directly at the database, so I know that the roles are there and are
    associated with the users. Why am I able to use Membership but not Roles? I
    would think that if there was a problem with the setup I would recieve an
    Exception or error rather than just be returned an empty array. Any help
    would be greatly appreciated. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Aug 9, 2008
    #1
    1. Advertising

  2. Re: System.Web.Security.Roles.GetRolesForUser() is not returning thecorrect results

    On Aug 9, 1:34 am, "Nathan Sokalski" <> wrote:
    > When I call System.Web.Security.Roles.GetRolesForUser() it returns no
    > results (an array of length 0) even though I have roles associated with the
    > currently logged in user. I am able to get the username by calling
    > System.Web.Security.Membership.GetUser().UserName. Why am I unable to get
    > the Roles, and what could I need to change? The <membership> and
    > <roleManager> elements in my Web.config are as follows:
    >
    >     <membership defaultProvider="CustomizedProvider">
    >       <providers>
    >         <add name="CustomizedProvider"
    >              type="System.Web.Security.SqlMembershipProvider"
    >              connectionStringName="logindb"
    >              applicationName="Membership"
    >              minRequiredPasswordLength="8"
    >              minRequiredNonalphanumericCharacters="0"
    >              requiresQuestionAndAnswer="false"
    >              requiresUniqueEmail="false"/>
    >       </providers>
    >     </membership>
    >
    >     <roleManager enabled="true" defaultProvider="CustomizedRoleProvider">
    >       <providers>
    >         <add name="CustomizedRoleProvider"
    >              type="System.Web.Security.SqlRoleProvider"
    >              connectionStringName="logindb"
    >              applicationName="Membership"/>
    >       </providers>
    >     </roleManager>
    >
    > Everything that I have tried with Membership has given me no trouble, but
    > Roles seems to want to pretend that there are no roles, even though I have
    > added the roles using the ASP.NET Website Administration Tool, and even
    > looked directly at the database, so I know that the roles are there and are
    > associated with the users. Why am I able to use Membership but not Roles?I
    > would think that if there was a problem with the setup I would recieve an
    > Exception or error rather than just be returned an empty array. Any help
    > would be greatly appreciated. Thanks.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/


    Hi Nathan

    I am not very familiar with this but looking at the documentation I
    see following:

    - Only the roles for the application that is specified in the
    ApplicationName property are retrieved.

    - If CacheRolesInCookie is true, then the results of the
    GetRolesForUser method may be returned from the role cache rather than
    the specified role provider.

    Does this tell you something?

    http://msdn.microsoft.com/en-us/library/8ak75t41.aspx
     
    Alexey Smirnov, Aug 9, 2008
    #2
    1. Advertising

  3. The CacheRolesInCookie idea did cross my mind before, but I tried it again
    anyway. I tried setting the CacheRolesInCookie="false" and deleting all my
    browser cookies, but that did not help, so if caching is the problem, I am
    still doing something wrong. I'm not sure what you meant by the
    applicationName thing, but I do have that property set in my <add/> element
    in the Web.config. In case the value of this property confused you,
    Membership is the name of my Web Application Project, which is why it is the
    same in both the membership and roleManager element providers. I've
    basically run out of ideas, so if anybody could help me, or even give a
    suggestion to try, it would be greatly appreciated. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Alexey Smirnov" <> wrote in message
    news:...
    On Aug 9, 1:34 am, "Nathan Sokalski" <> wrote:
    > When I call System.Web.Security.Roles.GetRolesForUser() it returns no
    > results (an array of length 0) even though I have roles associated with
    > the
    > currently logged in user. I am able to get the username by calling
    > System.Web.Security.Membership.GetUser().UserName. Why am I unable to get
    > the Roles, and what could I need to change? The <membership> and
    > <roleManager> elements in my Web.config are as follows:
    >
    > <membership defaultProvider="CustomizedProvider">
    > <providers>
    > <add name="CustomizedProvider"
    > type="System.Web.Security.SqlMembershipProvider"
    > connectionStringName="logindb"
    > applicationName="Membership"
    > minRequiredPasswordLength="8"
    > minRequiredNonalphanumericCharacters="0"
    > requiresQuestionAndAnswer="false"
    > requiresUniqueEmail="false"/>
    > </providers>
    > </membership>
    >
    > <roleManager enabled="true" defaultProvider="CustomizedRoleProvider">
    > <providers>
    > <add name="CustomizedRoleProvider"
    > type="System.Web.Security.SqlRoleProvider"
    > connectionStringName="logindb"
    > applicationName="Membership"/>
    > </providers>
    > </roleManager>
    >
    > Everything that I have tried with Membership has given me no trouble, but
    > Roles seems to want to pretend that there are no roles, even though I have
    > added the roles using the ASP.NET Website Administration Tool, and even
    > looked directly at the database, so I know that the roles are there and
    > are
    > associated with the users. Why am I able to use Membership but not Roles?
    > I
    > would think that if there was a problem with the setup I would recieve an
    > Exception or error rather than just be returned an empty array. Any help
    > would be greatly appreciated. Thanks.
    > --
    > Nathan Sokalski
    > ://www.nathansokalski.com/


    Hi Nathan

    I am not very familiar with this but looking at the documentation I
    see following:

    - Only the roles for the application that is specified in the
    ApplicationName property are retrieved.

    - If CacheRolesInCookie is true, then the results of the
    GetRolesForUser method may be returned from the role cache rather than
    the specified role provider.

    Does this tell you something?

    http://msdn.microsoft.com/en-us/library/8ak75t41.aspx
     
    Nathan Sokalski, Aug 9, 2008
    #3
    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?=

    Roles.IsUserInRole maps call to GetRolesForUser... Why?

    =?Utf-8?B?QmVuIFIu?=, Mar 20, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    931
    Steven Cheng[MSFT]
    Mar 24, 2006
  2. Nathan Sokalski
    Replies:
    2
    Views:
    507
    Nathan Sokalski
    Aug 5, 2008
  3. Nathan Sokalski
    Replies:
    2
    Views:
    1,625
    Nathan Sokalski
    Aug 9, 2008
  4. JaiPrakash Sharma

    Roles.GetRolesForUser(UserName) returning zero-dimension array

    JaiPrakash Sharma, Jan 30, 2008, in forum: ASP .Net Security
    Replies:
    1
    Views:
    234
    Misbah Arefin
    Feb 4, 2008
  5. Nathan Sokalski
    Replies:
    2
    Views:
    1,021
    Nathan Sokalski
    Aug 5, 2008
Loading...

Share This Page