Custom Login Web Part In SharePoint 2010 Forms Based

This post explains How To Create Custom Login Web Part In SharePoint 2010 For Forms Based Authentication FBA.

Before creating Login WebPart we need to setup and configure Membership Provider Database and Forms Based Authentication(FBA) In SharePoint 2010 and can optionally Allow Anonymous Access to sharepoint web application and sites collection.

1. Create Visual Web Part In Visual Studio 2010
Open VS 2010 and create new project by selecting New > Project from File Menu

Create Custom Login WebPart In SharePoint 2010 For Forms Based Authentication FBA


Choose SharePoint 2010 in left pane and select Empty SharePoint Project, name it LoginWebPart.

Select Deploy as a farm solution option from next screen and click on finish

Right click on solution explorer and select Add > New Item

Visual Web Part For Login In SharePoint 2010


Choose Visual Web Part from the list and name it LoginWebPart

2. Add Reference To Assemblies
2a. Add reference to Microsoft.SharePoint.IdentityModel.dll
Right click on Refrences in solution explorer, select add reference and browse to
C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint.IdentityModel\14.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.IdentityModel.dll 

2b. Add reference to Microsoft.IdentityModel.dll
Select add reference and browse to
C:\Program Files\Reference Assemblies\Microsoft\Windows Identity Foundation\v3.5\Microsoft.IdentityModel.dll

2c. Add reference toSystem.IdentityModel.dll
Browse to C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.IdentityModel.dll

2d. Add Microsoft.SharePoint.IdentityModel assembly reference in Page Directive of LoginWebPartUserControl.ascx

Assembly reference to Microsoft.IdentityModel.dll


3. Place Login Control on the LoginWebPartUserControl

HTML SOURCE
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
 
<%@ Assembly Name="Microsoft.SharePoint.IdentityModel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
 
<%@ 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" %>
 
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" 
             Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
 
<%@ Import Namespace="Microsoft.SharePoint" %> 
 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" 
             Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LoginWebPartUserControl.ascx.cs" 
            Inherits="LoginWebPart.LoginWebPart.LoginWebPartUserControl" %>
 
 
<asp:Login ID="Login1" runat="server" 
           FailureText="<%$ Resources:wss,login_pageFailureText %>" 
           onauthenticate="Login1_Authenticate">
</asp:Login>


4. Go to Code behind of user control and add following reference
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.IdentityModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.IdentityModel.Tokens;


Write following code in Login1_Authenticate Event of Login Control

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            string membership = "MembershipProvider";
            string role = "RoleProvider";

            e.Authenticated = SPClaimsUtility.AuthenticateFormsUser(new Uri(SPContext.Current.Web.Url),Login1.UserName,Login1.Password);

            if (!e.Authenticated) return;

            SecurityToken token = SPSecurityContext.SecurityTokenForFormsAuthentication(new Uri(SPContext.Current.Web.Url), membership, role, Login1.UserName, Login1.Password);
            if (token == null)
            {

                e.Authenticated = false;
                return;
            }
            else
            {

                SPFederationAuthenticationModule module = SPFederationAuthenticationModule.Current;
                module.SetPrincipalAndWriteSessionToken(token);
                e.Authenticated = true;

                SPUtility.Redirect(SPContext.Current.Web.Url, SPRedirectFlags.Trusted, this.Context);

            }
        }


Build and Deploy Solution by clicking on Build Menu and select Deploy Solution From Visual Studio

5. Open SharePoint site in browser and login with administrator account

Click on Site Actions and Select Edit Page



Click on Insert and select Web Part from the top ribbon



Select Custom from Categories And select LoginWebPart, Click on Add and Save And Close





Hope this helps

If you like this post than join us or share

5 comments:

Anurag Soni said...

Custom Login Web Part In SharePoint 2010 Forms Based


Priya said...

Hi i have done detto the same what you provide the code.But i facing with 2 erors as "Error 2 No overload for method 'SecurityTokenForFormsAuthentication' takes 3 arguments
&Error 1 The name 'wss' does not exisit in current context

Can you please suggest me what mite be the wrong..
Thanks in advance..



Priya said...

Error 1
The name 'wss' does not exist in the current context

Error 2
No overload for method'SecurityTokenForFormsAuthentication' takes 3 arguments


Priya said...

Thankkks In advance..Im struggling to recitfy this errors from many days. !except project name everycode is detto wat u mentioned.but i facing with those errorss ! Help please


Unknown said...

Hi

I have the requirement like this and wanted to use the webpart as login as i have so much content in the login and site admin has to modify that content in web part. I followed this article, but here is my doubts. If i give the custom login page url of start page how user can see this page before login to the site. My authentication provider is active directory with custom login. Please reply


Find More Articles