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:

Published Saturday, January 09, 2010 9:02 PM by guru_sarkar

Comments

# Asp.Net mebership creates two users in aspnet_Users table

Saturday, January 09, 2010 10:06 PM by Guru Sarkar's Blog

While using asp.net membership and roles you will see two seperate entries for same user in aspnet_Users

# re: Asp.Net Roles not working when site published on server

Thursday, March 18, 2010 7:44 PM by BladeRunner26

I have created a web project where I am using .net membership out of the box, configured it correctly.

The authentication through membership and roles works when I publish the site to my hosting provider with the data source configured in the web.config, and visit a protected resource on the site, I get automatically redirected to my specified login page, login, and get redircted back to the original page that was requested (assuming I am currently in a role that is authorized to view that resource).

However, when I launch the exact same project from Visual Studio 2008 (F5) with no changes made at all to any configuration files, etc... pointing to the same remote data source that houses my asp_membership/role database, I am not requested to login and the protected resource (*.aspx) is provided without asking me to login.

As a test I qualified one of my Business Layer Class objects with:

[PrincipalPermission(SecurityAction.Demand, Authenticated = true)]

...it will in fact throw a security exception. So it is clear that I am not authenticated, but without this line, the page will load without requireing me to logon.

So for some reason, on my localhost, local development machine, when I run the exact same project that is working on my hosting provider locally, I am not asked to login/authenticate (redirected to the login page when unauthenticated and requesting a secured aspx file), but assertions like the one above do identify me as not authenticated, and work as expected.

Despite the fact that clearly I am not authenticated, just to be sure, I have closed browsers, cleared cookies, etc... however nothing works to get the security membership working, locally running through VS2008, again though it works on the remotely hosted site perfectly.

Obviously this is fustrating, as most of the time people have the exact opposite problem (working locally, but not after publish/copy). So I haven't found any posts on why this would happen. This is making it extremely difficult if not impossible to test, however, once the project goes live, I won't be able to test obviously.

IIS is installed locally, so maybe some weird conflict that I am not aware of with launching from VS2008. Any ideas experts?

Thank you in advance for your help.

# re: Asp.Net Roles not working when site published on server

Thursday, March 25, 2010 10:34 AM by guru_sarkar

BladeRunner26,

Sorry, missed your comment but I think you found the answer.

# re: Asp.Net Roles not working when site published on server

Friday, January 14, 2011 1:05 AM by Raj

Perfect! Exactly what I needed. Wish I found this hours ago....

# ASP.NET MVC Archived Buzz, Page 1

Wednesday, March 09, 2011 9:19 PM by ASP.NET MVC Archived Buzz, Page 1

Pingback from  ASP.NET MVC Archived Buzz, Page 1

Leave a Comment

(required) 
(required) 
(optional)
(required)