.NET Tip of The Day
Learn one new .NET trick every day
Login or Join
.NET Tips & Tricks Community RSS

Those who prefer reading this site through RSS now can also subscribe for updates to .NET Tips & Tricks Community.

Always set the "applicationName" property when configuring Membership and other Providers

Users created with the membership API will be associated with applicationName value specified in provider declaration in web.config file. This diagramm shows how user is associated with applicationName in database:

Relations between aspnet_Users and aspnet_Applications

When no applicationName attribute is configured, ASP.NET uses the application vroot path within the web-server to automatically calculate the applicationName to use when adding data to an ASP.NET Application Service database.

Now let's assume you develop an ASP.NET 2.0 application locally using Membership, Roles or Profile features and you haven't specified applicationName attribute. You create several new users. Because applicationName property was not specified your users were associated with auto calculated value (something like "/WebSite1").

This works fine when the application continues to run in the "/WebSite1" application virtual path. But if it is copied to another location or server with a different virtual path (for example: "/app1" or more commonly just "/"), then when the Membership APIs are used they will not "see" the users already in our database – since they will lookup membership data using a different application name and filter the users in the application_Users table accordingly. That is why you'll get a "Login attempt unsuccessful, please try again." message when you try to login.

The best way to prevent this from ever happening is to always specify the "applicationName" attribute when declaring your providers. One good default value to use is "/" – which is the root application name. This is the value specified for the default provider that ships with ASP.NET 2.0 (which by default stores the application service data within the ASPNETDB.MDF file under /app_data), and is why if you don't override the provider settings it will work if you copy an app to another machine.



P.S. The reason why the applicationName setting even exists in the first place is so that you can map multiple applications and sites to the same database.

8/9/2007
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.