How to create custom login page for SharePoint 2010 form based authentication (FBA)

I went through Steve Peschka’s blog explaining how to set up FBA with LDAP provider in SharePoint 2010:

http://blogs.technet.com/b/speschka/archive/2009/11/05/configuring-forms-based-authentication-in-sharepoint-2010.aspx.

What a wonderful post. It describes all the steps precisely and with clear instructions. I configured FBA in no time after going through the post.

Next thought came to my mind was how to get rid of the OOB login page (“_forms/default.aspx”) and use my own. Again I found another post from Kirk Evans blog that did the magic:

http://blogs.msdn.com/b/kaevans/archive/2010/07/09/creating-a-custom-login-page-for-sharepoint-2010.aspx

It describes how to create your own login page that is somewhat similar to “_forms/default.aspx” and you will use a login control to accomplish the task.

Everything went good so far. But what if I want to add my own form with userid and password fields and do not want to use “_layouts/simple.master”? I dug a little further and found “Microsoft.SharePoint.IdentityModel” namespace has “SPClaimsUtility.AuthenticateForm” method that would do the work for me.

I created another application page within the same project I used for creating login page as given by Kirk Evans blog. After little modification this is how the login page and its background code look like (Deployment option is same as given by Kirk Evans blog):

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

<%@ Page Language="C#" AutoEventWireup="true" Inherits="LdapContosoAuthentication.Layouts.LdapContosoAuthentication.LoginCustmCntrlPage" CodeBehind="LoginCustmCntrlPage.aspx.cs"%>

<html>

<head runat="server">

<title>

Login Page

</title>

<style type="text/css">

    .style1

    {

        width: 100%;

    }

</style>

</head>

<body>

<form id="form1" runat="server">

<div>

<table class="style1">

    <tr>

        <td>

            UserID</td>

        <td>

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        </td>

    </tr>

    <tr>

        <td>

            Password</td>

        <td>

            <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>

        </td>

    </tr>

    <tr>

        <td>

            <asp:Label ID="Label1" runat="server"></asp:Label>

        </td>

        <td>

            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />

        </td>

    </tr>

</table>

</div>

</form>

</body>

</html>

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.IdentityModel;

namespace LdapContosoAuthentication.Layouts.LdapContosoAuthentication

{

    public partial class LoginCustmCntrlPage : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            bool status = SPClaimsUtility.AuthenticateFormsUser(Context.Request.UrlReferrer, TextBox1.Text, TextBox2.Text);

            if (!status)

            {

                Label1.Text = "Wrong Userid or Password";

            }

            else

            {

                if (Context.Request.QueryString.Keys.Count > 1)

                {

                    Response.Redirect(Context.Request.QueryString["Source"].ToString());

            }

                else

                    Response.Redirect(Context.Request.QueryString["ReturnUrl"].ToString());

            }

        }

    }

}

You can download the VS 2010 solution from here. I also tried to use default masterpage from any site instead of “_layouts/simple.master” in another custom login page. But it seems not possible as you are still not logged in. One alternative is having similar masterpage in layouts folder and use it from there.

 

 

LdapContosoAuthentication.zip