SharePoint 2010: Create Custom Login page with persistent cookies


At my current client we are using FBA ( Forms Based Authentication) with LDAP membership provider.

The issue was the users were prompted the login page( Dual Authentication) while opening Office documents( Office Client) , something below with setting in Web Application to default sign in page.

 

Some history on the issue and need to create a custom login page to enable persistent cookies.

For search I had to enable Windows Authentication and that’s why I get to select between Windows and Forms Auth…

image 

image

Selecting Forms Authentication takes me to _forms/default.aspx which looks like

image

If you select the checkbox “sign me in automatically” you wont see the ugly pop up for dual authentication when you are opening office documents.

image

To have get away from this pop up you could check the “Sign me in automatically” or write a custom login page to enable persistent cookies

Steps to create a custom login page to enable persistent cookies:

  1. Create a VS 2010 Sharepoint project like below:

    customlogin

  2. Copy the contents from the _forms/default.aspx into your login.aspx
    <%@ Assembly Name="Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ Assembly Name="CGAP.Intranet.CustomLoginPage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=401c0003603ebc24" %>

     

    <%@ Page Language="C#" Inherits="Intranet.Login"

    MasterPageFile="~/_layouts/Master Pages/Login.master" %>

     

    <%@ Import Namespace="Microsoft.SharePoint.WebControls" %>

    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"

    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <%@ Import Namespace="Microsoft.SharePoint" %>

    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">

    <SharePoint:EncodedLiteral runat="server" EncodeMethod="HtmlEncode" ID="ClaimsFormsPageTitle" />

    </asp:Content>

    <asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"

    runat="server">

    <SharePoint:EncodedLiteral runat="server" EncodeMethod="HtmlEncode" ID="ClaimsFormsPageTitleInTitleArea" />

    </asp:Content>

    <asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderSiteName" runat="server" />

    <asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderMain" runat="server">

    <div id="SslWarning" style="color: red; display: none">

    <SharePoint:EncodedLiteral runat="server" EncodeMethod="HtmlEncode" ID="ClaimsFormsPageMessage" />

    </div>

    <%
       1: --div style="display: none">

       2: <script language="javascript">

       3: if (document.location.protocol != 'https:') {

       4: var SslWarning = document.getElementById('SslWarning');

       5: SslWarning.style.display = '';

       6: }

       7: </script>

       8: </div>--

    %>

    <div align="center">

    <table border="2" style="border-width: thin; border-color: #9c8a7b; width: 300px">

    <tr>

    <td>

    <asp:Login ID="signInControl" FailureText="<%$Resources:wss,login_pageFailureText%>"

    runat="server" Width="100%">

    <LayoutTemplate>

    <asp:Label ID="FailureText" class="ms-error" runat="server" />

    <table class="ms-input" cellspacing="2" cellpadding="5">

        <tr>

            <td nowrap="nowrap">

                <SharePoint:EncodedLiteral ID="EncodedLiteral3" runat="server" Text="<%$Resources:wss,login_pageUserName%>"

                    EncodeMethod='HtmlEncode' />

            </td>

            <td>

                <asp:TextBox ID="UserName" autocomplete="off" runat="server" class="ms-inputuserfield"

                    Width="150px" />

            </td>

        </tr>

        <tr>

            <td nowrap="nowrap">

                <SharePoint:EncodedLiteral ID="EncodedLiteral4" runat="server" Text="<%$Resources:wss,login_pagePassword%>"

                    EncodeMethod='HtmlEncode' />

            </td>

            <td>

                <asp:TextBox ID="password" TextMode="Password" autocomplete="off" runat="server"

                    class="ms-inputuserfield" Width="150px" />

            </td>

        </tr>

        <tr>

            <td>

                <a id="lnkForgotPassword" href="/_layouts/Intranet.CustomLoginPage/ForgotPassword.aspx">Forgot Password</a>

            </td>

            <td style="text-align: right">

                <a id="lnkChangePassword" href="/_layouts/Intranet.CustomLoginPage/ChangePassword.aspx">Change Password</a>

            </td>

        </tr>

        <tr>

            <td colspan="2" align="right">

                <asp:Button ID="login" CommandName="Login" Text="<%$Resources:wss,login_pagetitle%>"

                    runat="server" />

            </td>

        </tr>

        <tr>

            <td colspan="2">

                <asp:CheckBox ID="RememberMe" Text="<%$SPHtmlEncodedResources:wss,login_pageRememberMe%>"

                    runat="server" Checked="true" />

            </td>

        </tr>

    </table>

    </LayoutTemplate>

    </asp:Login>

    </td>

    </tr>

    </table>

    </div>    

    </asp:Content>

  3. In the code behind you need derive from FormsSignInPage
    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using Microsoft.SharePoint.IdentityModel;

    using Microsoft.SharePoint.IdentityModel.Pages;

     

     

    namespace CGAP.Intranet

    {

        public partial class Login : FormsSignInPage

        {

            protected void Page_Load(object sender, EventArgs e)

            {

                signInControl.RememberMeSet = true;

     

            }

     

        }

    }

  4. So basically what we are doing here is having SharePoint taking care of authentication by setting the RememberMeSet” property as true on the signInControl
  5. Make the changes in the Central Administration for setting the custom login page setting for the Forms Based Authentication
  6. You would see the custom login has the “Sign me in automatically”
  7. Try opening a word document from site and you WON’T be asked for your credentials to the login form.

image

Have fun cheers!

 

************UPDATE: Login master as requested by folks************

<%@ Master Language="C#" %>

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"

    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>

<%@ Register TagPrefix="wssuc" TagName="TopNavBar" Src="~/_controltemplates/TopNavBar.ascx" %>

<%@ Register TagPrefix="wssuc" TagName="Welcome" Src="~/_controltemplates/Welcome.ascx" %>

<%@ Register TagPrefix="wssuc" TagName="MUISelector" Src="~/_controltemplates/MUISelector.ascx" %>

<%@ Register TagPrefix="wssuc" TagName="DesignModeConsole" Src="~/_controltemplates/DesignModeConsole.ascx" %>

<html id="Html1" lang="<%$Resources:wss,language_value%>" dir="<%$Resources:wss,multipages_direction_dir_value%>"

xmlns:o="urn:schemas-microsoft-com:office:office" runat="server">

<head id="Head1" runat="server">

    <meta name="GENERATOR" content="Microsoft SharePoint">

    <meta name="progid" content="SharePoint.WebPartPage.Document">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <meta http-equiv="Expires" content="0">

    <meta name="ROBOTS" content="NOHTMLINDEX">

    <title id="onetidTitle">

        <asp:ContentPlaceHolder ID="PlaceHolderPageTitle" runat="server" />

    </title>

    <SharePoint:CssLink ID="CssLink1" runat="server" Alternate="false" />

    <SharePoint:ULSClientConfig ID="ULSClientConfig1" runat="server" />

    <SharePoint:ScriptLink ID="ScriptLink1" Language="javascript" Name="core.js" EnableCustomActions="false"

        runat="server" />

    <asp:ContentPlaceHolder ID="PlaceHolderAdditionalPageHead" runat="server" />

</head>

<body scroll="yes" onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();">

    <form id="Form1" runat="server" onsubmit="if (typeof(_spFormOnSubmitWrapper) != 'undefined') {return _spFormOnSubmitWrapper();} else {return true;}">

    <asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="false" EnablePartialRendering="true"

        EnableScriptGlobalization="false" EnableScriptLocalization="true" />

    <SharePoint:SPNoScript ID="SPNoScript1" runat="server" />

    <table class="ms-main" cellpadding="0" cellspacing="0" border="0" width="100%">

        <tr style="height: 20px">

            <td colspan="3" style="background-image: none; background-color: #dedfde" />

        </tr>

        <tr style="height: 75px">

            <td style="background-image: none; background-color: #f78e18; width: 15px" />

            <td style="background-image: none; background-color: white; width: 25px" />

            <td>

                <img id="Img1" src="/_layouts/Intranet/images/IntranetLogo.jpg" alt="" />

            </td>

        </tr>

        <tr style="height: 0.5px">

            <td colspan="3" style="background-image: none; background-color: #9c8a7b" />

        </tr>

    </table>

    <div style="vertical-align: middle">

        <table style="width: 100%; height: 80%;">

            <tr style="vertical-align: middle; height: 100%">

                <td style="width: 25%" />

                <td style="width: 50%; vertical-align: middle">

                    <asp:ContentPlaceHolder ID="PlaceHolderMain" runat="server" />

                </td>

                <td style="width: 25%" />

            </tr>

        </table>

    </div>

    <div style="display: none">

        <table>

            <tr>

                <td>

                    <asp:ContentPlaceHolder ID="PlaceHolderSiteName" runat="server" />

                    <asp:ContentPlaceHolder ID="PlaceHolderTitleBreadcrumb" runat="server" />

                    <asp:ContentPlaceHolder ID="PlaceHolderPageTitleInTitleArea" runat="server" />

                </td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

About these ads

Tags: ,

10 Comments on “SharePoint 2010: Create Custom Login page with persistent cookies”

  1. Roberto December 17, 2010 at 12:21 pm #

    Hi Atul, you can share CustomLoginPage project???
    Ps. Images are broken on certain pages

  2. atulchhoda December 17, 2010 at 2:39 pm #

    Roberto, I will upload the login page sometime today. Images are loading for me so I guess there is something blocking on your ntw/browser.

  3. Kaléu December 23, 2010 at 1:04 pm #

    Hi Atulchhoda. Great work!
    How can I change the _layouts/Authenticate.aspx page? All I want is change the “X” picture and write few things.

    Thx!

    • atulchhoda December 23, 2010 at 5:00 pm #

      Hey there you would have to change that in the master page. The code I uploaded uses the OOB layout master page i.e. simple.master

      Create your own master page and set it as below in the login.aspx

  4. Todd January 10, 2011 at 8:53 pm #

    Would you be willing to post up your login.master? I cannot for the life of me figure out where simple.master causes the red X to appear.

  5. atulchhoda January 18, 2011 at 9:45 pm #

    Todd use the master I updated without figuring for the life of …

  6. Vinny Tang October 26, 2011 at 4:03 pm #

    I sent the signInControl.RememberMeSet = true and it does not appear to be running the load event. I even checked the value in the rememberme to true it’s not checked? Any ideas?

  7. susheel June 1, 2012 at 5:04 am #

    can i have a seperate login page for only active directory users in sp2010

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 81 other followers

%d bloggers like this: