15.6. Supporting Dynamic Applications

The RoleProvider base class defines the abstract property ApplicationName. As a result, you can use the same approach for supporting multiple applications on-the-fly with SqlRoleProvider as was shown earlier for SqlMembershipProvider. After you have a way to set the application name dynamically on a per-request basis, you can write a custom version of SqlRoleProvider that reads the application name from a special location. Remember that in Chapter 12 an HttpModule was used that looked on the querystring for a variable called appname. Depending on the existence of that variable as well as its value, the module would store the appropriate application name in HttpContext.Items["ApplicationName"]. You can use the same module with a custom version of the SqlRoleProvider.

C#

using System;
using System.Web;
using System.Web.Security;

public class CustomRoleProvider : SqlRoleProvider
{
    public override string ApplicationName
    {
        get
        {
            string appNameFromContext =
                (string)HttpContext.Current.Items["ApplicationName"];
            if (appNameFromContext != "NOTSET")
                return appNameFromContext;
            else
                return base.ApplicationName;
        }
    }
}

VB.NET

Imports Microsoft.VisualBasic Imports System Imports System.Web Imports System.Web.Security Public Class CustomRoleProvider Inherits SqlRoleProvider Public Overrides Property ApplicationName() As String Get Dim appNameFromContext As String = _ CStr(HttpContext.Current.Items("ApplicationName")) If appNameFromContext <> "NOTSET" Then ...

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.