Saturday, April 11, 2009

ASP.NET 2.0: Creating Portal like applications using Membershi...

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



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 // thanks Kieran for pointing this out

{

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
















connectionStringName=”LocalSqlServer” />







* 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!

No comments:

Post a Comment