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

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

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

Alexey Smirnov

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.

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
 
N

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/

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.

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
 

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

Top