This article is for people who are pretty familiar with ASP.NET 2.0 Membership Api. I am going to talk briefly about Membership API and then move towards portal kind of application.

                                      
Introduction:

This article is for people who are pretty familiar with ASP.NET 2.0 Membership Api. I am going to talk briefly about Membership API and then move towards portal kind of application.

The Membership API:

Membership API has username and application name as a composite primary key. This is provider-based approach so it typically consists of Application Name attribute in all possible cases. For most of the web applications, during development mode machine.config sets "/" or "/webfoldername" depending on which web server you are using . The one restriciton on the applicationName attribute though is that it is statically defined. Once you set the value in configuration, the provider remembers that value for the rest of its lifetime. Here is the fun part, if you take a look at the Membership Provider base class, applicationname property is abstract and a setter is also defined. Yes, I agree you don't normally see that. But the beauty is, it gives us power to change the applicationname at runtime. But it is not as simple as we thought to get it working as a portal application. Here are the following steps that will help you achieve portal like behavior. I have converted dozens of application using this approach. Its proven and it works.

Step #1

Create a class name PortalHttpModule that implements IHttpModule in App_Code folder. Here is the content
 of that class.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class PortalHttpModule :IHttpModule
{
        public void Dispose()
        { return; }
        private void DetermineApplicationName(Object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            HttpContext context = app.Context;
            string qAppName = app.Request.QueryString["appname"];
            if (!String.IsNullOrEmpty(qAppName))
                context.Items["ApplicationName"] = qAppName;
            else
                context.Items["ApplicationName"] = "NA";
        }
        public void Init(HttpApplication app)
        {
            app.BeginRequest +=
                new EventHandler(this.DetermineApplicationName);
        }
}

The next step is to write a custom provider that overrides the applicationName property getter. This will also be placed in App_Code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

Last but not least. We have to add couple lines of code in web config. Here it is

<httpModules>

<add name =”PortalProcessor” type=”PortalHttpModule”/>

</httpModules>
<membership defaultProvider=”portalApp”>

<providers>
    <add name=”portalApp” type=”ApplicationProvider”
    connectionStringName=”LocalSqlServer” />
</providers>

</membership>


Create a page called create user. Drag & Drop Create user wizard control. Save it

Open your browser and request following URL
http://localhost/yourappname/createuser.aspx?appname=alyb-rocks
or
http://localhost/yourappname/createuser.aspx?appname=tookoolforschool
Try creating user through wizard control and take a look at db. You will see different applicationID

I currently use this applicationname as my subdomain using wildcard subdomain. It is very easy to set up. Most domain registrars give you wildcard domain which means that all your subdomain will point to one web application in that case you can replace
string qAppName = app.Request.QueryString["appname"];
with
string qAppName = app.Context.Request.ServerVariables["Host"].Substring(0, app.Context.Request.ServerVariables["Host"].IndexOf("."));

Enjoy!

About the Author:

My name is Aly Boghani, I am from originally from Hyderabad, Pakistan but currently living in Chicago. I went to DePaul University(Chicago, IL) for undergrad and majored in Software Engineering. My focus was on pure software development. Currently I am pursuing my Masters in Business & IT from Northwestern University(Evanston, IL). I have been programming since I was 15 years old. I have currently been working as a Sr. Application Developer for a small business named Switchfast Technologies where I manage a team of several onshore and offshore developers.