By: MJ Ferdous | Comments | Related: > SharePoint Administration
Problem
Many applications need Claim Based Authentication instead of Classic Mode Authentication. The Form Based Authentication (FBA) is one type of Claim Based Authentication. The default login page of FBA in SharePoint 2010 is very simple which only provides a simple Login control with the user name, password and remember me option. But many developers want to implement a customized login page for a better looking experience or more options according to business requirements.
Solution
This article will demonstrate how to create a custom login page for a SharePoint site using Visual Studio 2010 and deploy it in a server farm. It will also include how to change the default login page URL from the Central Administration Application Provider Settings.
Step 1:
Create a new project type "Empty SharePoint Project" in Visual Studio 2010 as showed below:
Provide a Project name and Press "OK". You will be prompted with a Wizard to specify the SharePoint site where you want to deploy the custom login page. You can validate the site URL to ensure that your site is fine to deploy in this solution. Remember that you have to choose Deploy as Server Farm solution instead of deploying as a sandboxed solution (default selected as below). Otherwise you will get an error when you deploy the solution:
"The deployment type "TemplateFile" of file "CustomLogin.aspx" in the Project Item "Layouts" is not compatible with a Package in a Sandboxed Solution". Because Layout is root place to deploy in SharePoint server farm which is mapped {SharePointRoot}\Template\Layouts.
So you will select Deploy as Server Farm solution after validation of the site URL.
Then "Click to Finish" and the project creation should be successful.
Step 2:
You have to add a new Application Page with a right-click on the project Solution Explorer -> New item to create Custom Login page
The form will auto-generate some code where you:
- Need to remove asp:Content ID="Content1" runat="server" from the CustomLogin.aspx
- Need to remove DynamicMasterPageFile="~masterurl/default.master" from the @Page Register tag
- Write simple HTML code for the Login Box user interface including username and password textboxes anda Login Button like below:
Step 3:
- Add the Microsoft.SharePoint.IdentityModel DLL reference from .\assembly\GAC_MSIL\Microsoft.SharePoint.IdentityModel\14.0.0.0__71e9bce111e9429c \Microsoft.SharePoint.IdentityModel.dll to access SPClaimsUtility
- Include using Microsoft.SharePoint.IdentityModel; at CustomLogin.aspx.cs
- Change Inheritance LayoutsPageBase to System.Web.UI.Page
- Execute the following code inside the Login button click event as below:
bool status = SPClaimsUtility.AuthenticateFormsUser( Context.Request.UrlReferrer, UserName.Text, Password.Text); if (!status)// if auth failed { lblError.Text = "Wrong Userid or Password"; } else //if success { Response.Redirect("http://fba.contosto.com"); //Use site url }
Step 4:
Build the solution. Then Deploy the solution package which will create a folder inside .\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS\CustomLoginPageFBA
Step 5:
Change the custom page URL using following steps:
- Go to Central Administration.
- Select Site from Application Management.
- Click on Authentication Provider.
- Select Zone and go inside it to get the following Sign In Page URL Section.
- Change Custom Sign In Page URL.
Next Steps
- Download the complete source code from here
- Check for a future article on how to register a new user for FBA authentication.
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips