Wednesday, 17 August 2011

Custom Login page for Sharepoint 2010 Mixed mode authentication

In one of my previous post, I discussed about adding form authentication to your SharePoint site. I Hope it was useful ! This post is dedicated to customize the Login Screen for the SharePoint site

Recently I had to configure Windows authentication and forms authentication in SharePoint 2010. It should not have been difficult in 2010 as it have mixed mode authentication. But what lacked were enough tutorials/articles. So I thought to share it!
First of all we need a landing page for our application. It is good to have common landing page for windows authentication and SharePoint authentication. So that every unauthenticated request can be re-directed to single page. By default SharePoint have a dropdown to select the authentication mode and then proceeds based on selection.
Custom Login page for Sharepoint 2010 Mixed mode authentication
We can also have our own login page with the same functionality. I have designed something like this.
Custom Login page for Sharepoint 2010 Mixed mode authentication

You have to specify the login page URL in web.config. By default it is “/_login/default.aspx”
<forms loginUrl="/_layout/MyLoginPage.aspx" />
Note : MyLoginPage.aspx should be application page as user will not have access to SharePoint page,  as he /she is not authenticated yet.
As now as we have our login page ready, we need to add the code for login button and hyperlink to intranet access
1.       Forms Authentication via Login Button
2.       Windows Authentication via hyperlink ‘Click here for intranet access’

1.       Forms Authentication
In the event handler of login click event we can authenticate the user with the following method.
This method authenticates a form user by checking username and password. When the user is validated this object persists a cookie to the client for later use.

2.       Windows Authentication
LnkIntranet.NavigateUrl = Page.ResolveUrl(@"~/_windows/default.aspx?ReturnUrl=/YouLandingPage.aspx");

SharePoint will authentication the windows logged in user
On Success it will redirect to the ‘ReturnUrl’ page mentioned in the parameter
On Error it will redirect to ‘AccessDenied.aspx’