12.12. Supporting Dynamic Applications

Normally, an instance of SqlMembershipProvider knows which application name to use by looking at the value of the applicationName configuration attribute. The default configuration in machine.config sets applicationName to /, so most developers will probably want to explicitly redefine membership providers in their applications to use a more suitable name. Many of the previous examples of extending SqlMembershipProvider showed configurations that used more appropriate values for applicationName.

The one constraint on the applicationName attribute, though, is that it is statically defined. After you set the value in configuration, the provider remembers that value for the rest of its lifetime. If you look at the MembershipProvider base class definition, though, you see that the ApplicationName property for the provider is abstract and that a setter is also defined. Concrete providers like SqlMembershipProvider can choose to implement the setter so that developers can change the application name at runtime.

This means that you can write code that switches between different application data living in the same Membership table with code like the following:

C#

p = (SqlMembershipProvider)Membership.Provider;  //assume default provider is SQL
p.ValidateUser("someuser","somepassword");

p.ApplicationName = "A_Different_Value_Than_Configuration";

p.ValidateUser("some other user", "password");

VB.NET

p = CType(Membership.Provider, SqlMembershipProvider) ...

Get Professional ASP.NET 3.5 Security, Membership, and Role Management with C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.