Asp.Net Roles not working when site published on server

Problem:

I have seen people complaining their asp.net user roles won't work as desired when they publish their site on server. Everything works locally when they run through Visual Studio.

Potential Reason:

applicationName attribute is missing from role provider in your web.config. e.g.

<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">

<providers>

<clear>

<add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer"

type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </providers>

</roleManager>

 When you don't set applicationName attribute asp.net uses your site's root path and set it in the aspnet_application table. So now if you move you site to server the root path may change and the roles provider will fail to find your user.

This has been discussed for membership provider here and same thing stands true for roles provider as well.

Solution:

Always set applicationName attribute for your membership and roles providers in your web.config.

Look for ApplicationName field in aspnet_Applications table in your membership database. Suppose it was set to "myApplication" by asp.net when you created roles then set that same value in your web.config.

<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">

<providers>

<clear>

<add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer"

applicationName="/myApplication"

type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> </providers>

</roleManager>

Points to note:

Since you have already created the roles and the application name is set in the aspnet_applications table, you can set your applicationName attribute to that value.

If you want to set it to something different then after setting the attribute in your web.confing you will have create your roles again and assing roles to your users appropriately.

Also note that there could be two or more application names in aspnet_applications table. May be because you have set the applicationName for your membership provider but not for roles provider. This will even create two user entries in aspnet_Users table for one single user. So if you want to have single entry in aspnet_Users table per user then you need to set applicationName to same value for both membership and roles provider.

Conclusion:

Don't forget to set applicationName attribute for membership and roles provider and that too with appropriate value.

Reference:

2 Comments

Comments have been disabled for this content.