JG Vimalan's Blog

It is all about coding!

Customize login in WSS 3.0/MOSS 2007 to redirect to a subsite site after login.

In this article we are going to see how to customize the login in WSS3.0/MOSS 2007 so that, when the user login in the parent site, they will be redirect to their respective sub site.

It is assumed that, you have forms authenication for your site and have multiple subsites.Earlier, I have noted the user login name and thier subsite urls in a database. So the concept is, a user can login only to his subsite. So, the basic idea is, a common url (parent url) provides a way to the users to login into their subsite.

See the image below for better understanding,



Browse to the path: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE. Locate the login.aspx file and open it in a development environment. I used VS.NET 2005.  

Step 2:   Add <%@ ImportNamespace = “System.Data,SqlClient”%> in the login.aspx  (This requires only if you are going to connect to a SQL Server database)

Step 3:   In the login control, include the OnLoggedIn event as shown below,  

<asp:login id=”login” OnLoggedIn=OnLoggedIn FailureText=………./>

Step 4:   Add the <script> tag between the namespace registration and the Content place holder and put some code as shown below,    

<script runat=server>

protected override void OnLoad(EventArgs e) 


base.OnLoad(e);   Page.Response.Write(this.Context.User.Identity.Name);  


void OnLoggedIn(object sender, EventArgs e) 


string connectionString = @”server=abc\SQLEXPRESS;database =         myDatabase;Integrated Security = SSPI”;     

using (SqlConnection conn = new SqlConnection(connectionString))  



SqlCommand cmd = new SqlCommand();

cmd.CommandText = “someProcedure”; 

cmd.CommandType = System.Data.CommandType.StoredProcedure;         cmd.Connection = conn;

SqlParameter param = new SqlParameter(“@UserName”, System.Data.SqlDbType.NChar, 256);        

param.Value = login.UserName; //Name in the login control text box                 


string userPortalName = Convert.ToString(cmd.ExecuteScalar());                 


if (!string.IsNullOrEmpty(userPortalName)) {            

string URL = “http://site/&#8221; + userPortalName.Trim() + “/default.aspx”;              Response.Redirect(URL);        




<asp:content ControlPlaceHolder………………/>

Step 5:  Key in the user name (who has access to a particular subsite) and password and login. 


Now, you will find that, the user is redirected to his subsite.   For example, if main url is, http://site/ then, he will be redirected to his subsite http://site/user10 . Now you have made a simple change in the login.aspx so that it behaves the way you like!!!


October 24, 2009 - Posted by | SharePoint


  1. Now that you know that 301 Redirect is the acceptable and best way to redirect files, pages and even domains, you may be curious to know how it works. Url Redirection Service

    Comment by Url Redirection Service | October 24, 2009 | Reply

  2. Just wanted to say thank you! for all the great info found on your site, even helped me with my work recently 🙂 keep it up!

    Comment by Spyware Blockers | November 18, 2009 | Reply

  3. good article

    Comment by jagan | April 6, 2010 | Reply

  4. I don’t usually reply to posts but I will in this case, great info…I will bookmark your site. Keep up the good work!

    Comment by Pat | March 9, 2011 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: