Sivarajan's Blog

SharePoint | Office 365 | Azure | JavaScript

SharePoint 2013 Enabling Custom Login Page and Mixed Contents - Part 2

Summary: In my previous post, I had walkthrough on how to create custom FBA with mixed mode authentication and here we will see that how to create and use the custom login page and also mixed contents (secured and unsecured) on same sites.

Enabling Anonymous Access for Mixed Contents

Some times we have an scenario to keep a secured and unsecured contents on same site. For implementing that follow the below steps.

  1. Open a Central Administration –> Application Management link
  2. Select the Web Application (in my case “Mixed Mode” http://spf2013:28317/ )  which one is created with mixed mode authentication. Follow my previous post to create a custom fba with mixed authentication.
  3. In top toolbar select the Authentication Provider and then select the Default option.
  4. Edit Authentication dialog box will be opened.
  5. Select the Enable Anonymous Access check box.
  6. Select the Custom Sign In Page option and then just enter the following URL  /SitePages/CustomLogin.aspx (Note: we will create this page later).
  7. In the same top toolbar, click the Anonymous Policy button.
  8. You can find different anonymous policies here. But I am selecting None for this walkthrough.
  9. Now browse the site collection http://spf2013:28317/ 
  10. Login using Site Collection Administrator account and browse the Site Settings –> Site Permissions.
  11. Select the Anonymous Access in the top toolbar and then select the Entire Web Site option in the Anonymous Access dialog box.customlogin1
  12. Sign Out the current login and navigate to home page. Now you can navigate the site pages and lists without Sign In.

    customlogin2

Creating a custom login page

Instead of creating a new SharePoint project, I am going to use the same application which was created for custom FBA  post.

  1. Open the CustomFBA project.
  2. Add the new Visual WebPart CustomLogin under the WebParts folder and add the following code
    CustomLogin.ascx
    <table>
        <tr>
            <td>
                Username
            </td>
            <td>
                <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" />
            </td>       
        </tr>

    </table>
    <asp:Label ID="lbError" runat="server"></asp:Label>
  3. Open the CustomDbMembershipProvider.cs file and create a new method CustomValidateUser method and move the code from ValidateUser method.
    CustomDbMembershipProvider.cs
    public override bool ValidateUser(string username, string password)
            {
                return true;
            }

            public bool CustomValidateUser(string username, string password)
            {
                try
                {
                    bool isAuth = false;

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        MemberShipProviderData memberShipProviderData = new MemberShipProviderData();
                        isAuth = memberShipProviderData.ValidateUser(username, password);
                    });

                    return isAuth;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
  4. Now open the CustomLogin webpart code behind file and add the following code.
    CustomLogin.ascx.cs
    protected void btnLogin_Click(object sender, EventArgs e)
            {
                try
                {
                    CustomDbMembershipProvider provider = new CustomDbMembershipProvider();
                    bool validateStatus = provider.CustomValidateUser(txtUsername.Text, txtPassword.Text);

                    if (validateStatus)
                    {
                        bool status = SPClaimsUtility.AuthenticateFormsUser(
                                Context.Request.UrlReferrer,
                                txtUsername.Text,
                                txtPassword.Text);

                        if (status)
                        {                        
                            HttpContext.Current.Response.Redirect("Home.aspx");
                        }                    
                    }
                }
                catch (Exception ex)
                {
                    lbError.Text = ex.Message;
                }
            }
  5. SharePoint will do the authentication easily, if you call the statement SPClaimsUtility.AuthenticateFormsUser method (it will internally invoke the ValidateUser method).
  6. But we assume that, there is an error inside the ValidateUser method and the error will be trapped by SPClaimsUtility.AuthenticateFormsUser and simply it will return false. For getting an actual exception, I am using the separate method for authentication.
  7. I am using a SharePoint foundation for this demo and now create a wiki page CustomLogin.aspx under the “SitePages” list and then insert the CustomLogin webpart.
  8. Now click the “Sign In” button and you can see the login page.

    customlogin3

SPContext.Current.Web.CurrentUser is always NULL

This is a common issue, if you are implementing a FBA for SharePoint sites. To solve this issue,

Code Snippet
public static SPUser GetLoggedUser()
        {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    return web.CurrentUser;
                }
            }        
        }

 

How I can login using Window User Account?

For some scenarios, we may require to login using Windows users (AD Users).  But custom FBA login will override the mixed mode selection option. Use the following URL for windows login 

http://<Site Collection URL>/_windows/default.aspx?ReturnUrl=%2f_layouts%2fAuthenticate.aspx%3fSource%3d%252F&Source=%2F

Conclusion

I hope that these post series may help you the following ways.

  1. Create the Custom FBA
  2. Replace the default login page
  3. Secured and unsecured content on same site.

Please write me if you are facing any difficulties in custom FBA implementation and you can find the sample here.

Comments (5) -

  • Gaurav Garg

    5/10/2013 5:54:52 AM | Reply

    Hi,

    I want to create a custom login page for my SharePoint 2013 portal. I am using Mix mode authentication (FBA using Sql server and Active Directory for local users). The above code is only for Sql Server authentication (FBA). will you please help me to find out the way by which i can authenticate the user from both the database (Sql server and Active Directory).

    Regards,
    Gaurav

  • Charlotte Tang

    12/31/2013 7:59:06 AM | Reply

    Hi Gaurav,

    HAPPY NEW YEAR!

    Thank you very much for setting up this great blog and sharing these articles on this topic. I have learned a lot of good tips from here.

    Is your post above the solution to a challenge which we are facing?

    •  We are using Project Online with SharePoint Online with Office 365.
    •  We are trying to find a solution to brand/ (if branding is not possible) get rid of the Office 365 log in page.
    •  What we are trying to achieve is:
    •  1. Users click on a link, say: https://abc.sharepoint.com/project1/
    •  2. Instead of seeing Office 365 login page, we would like them to be able to see the login page with our own brand or a login box to enter username and password.

    Thank you very much in advance for the time you set aside to share your knowledge with us.

    Sincerely, we really appreciate your kind help.

    Charlotte Tang
    charlottetang10@hotmail.com

  • Suresh

    1/22/2014 4:19:35 AM | Reply

    Hi Siva,

    I am using this SPSecurityContext.SecurityTokenForFormsAuthentication in custom login page to redirect. What i am actually doing is, from asp.net application i am redirecting Sharepoint without login prompt.

    I am sending User ID, password as query string to custom login page of sharepoint and using above method and redirecting, it working fine.

    but i need achieve the same without password. How should i do?

    Is it possible to pass, encrypted password to this method? if so then i can get from aspnetdb by user id.

    Thanks in advance.
    Suresh S

  • Whilda Chaq

    3/18/2014 9:43:26 PM | Reply

    Hi,

    this post was great!! it work so far for me.

    but, my purpose is just have 1 page which can access from anonymous to login, are you have an idea abaout that?

    because, in your post anonymous can access entire sharepoint page..

    thank's

Pingbacks and trackbacks (1)+

Add comment

Loading