O'Reilly logo

Professional ASP.NET 3.5 Security, Membership, and Role Management with C# and VB by Stefan Schackow, Bilal Haidar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required