Web parts and Form based authentication using SqlMembershipProvider

Here, in this article I want to focus on form based authentication with ASP.NET 2.0's Membership and How to implement Web parts in my sample project.

Here, we will start from step to step through coding. First in this article we will implement form based authentication using ASP.NET 2.0's Membership provider then we will implement Web part because web part is related with Membership.

 

SqlMembershipProvider - Storing User Account Data in a SQL Server Database
One of the two membership providers that ship with ASP.NET 2.0 is the
SqlMembershipProvider provider, which uses a SQL Server database to store authentication information. In order to use this provider you need to create the database schema used by the provider. There are two ways to accomplish this:

  1. Use the ASP.NET Website Administration Tool (will create the database schema in a new SQL Server 2005 database file ASPNETDB.mdf, placed in the application's App_Data folder)
  2. Use the ASP.NET SQL Server Registration Tool (aspnet_regsql.exe) command-line tool (use this tool to implement the schema in a SQL Server 2000 or 2005 database)

Here, in this project, we will not use ASPNETDB.mdf, so I think it is not necessary here to explain of how to use the ASP.NET Website Administration Tool.

We, will use customize database to complete this project. So we will use aspnet_regsql.exe tool for creating necessary tables in our own database for storing user account data and for Web parts implementation.

You’ll need to use the ASP.NET SQL Server Registration Tool (aspnet_regsql.exe) tool. This tool has a graphical component or can be used through the command-line. The graphical wizard allows you to specify the location to add the needed tables.

The SqlMembershipProvider stores user account information in two related tables:

  1. aspnet_Users
  2. aspnet_Membership

 

If we create the schema through the ASP.NET SQL Server Registration Tool, we'll need to manually customize the Web.config file. Additionally, if you place the schema in a database other than ASPNETDB.mdf in the App_Data directory, you'll need to customize the membership configuration in the Web.config, specifying the connection string to the database.

 

Step-1: Customizing the SqlMembershipProvider in Web.config file:-

File Name - Web.config

<?xml version="1.0"?>

<configuration>

          <appSettings/>

  <connectionStrings>

    <clear/>

    <add name="LocalSqlServer" connectionString="data source=192.168.201.69;database=PIMSTT;user id =sa ;password=pims"/>

  </connectionStrings>

          <system.web>

                   <compilation debug="true"/>

                   <authentication mode="Forms"/>

      <membership>

        <providers>

          <clear/>

          <add name="AspNetSqlMembershipProvider"

            type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"

            connectionStringName="LocalSqlServer"

            applicationName=" /webpartweb " />

        </providers>

      </membership>

      <profile enabled="true" defaultProvider="TableProfileProvider">

        <providers>

          <clear/>

          <add name="TableProfileProvider"

            type="Microsoft.Samples.SqlTableProfileProvider"

            connectionStringName="LocalSqlServer"

            table="asdspnet_Profile"

            applicationName=" /webpartweb "/>

        </providers>

      </profile>

  </system.web>

</configuration>

 

Step-2: Create CreateAccount.aspx Page:-

        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99"

            BorderStyle="Solid" BorderWidth="1px" CancelDestinationPageUrl="~/Default.aspx"

            ContinueDestinationPageUrl="~/Default.aspx" DisplayCancelButton="True" Font-Names="Verdana"

            Font-Size="10pt">

            <WizardSteps>

                <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">

                </asp:CreateUserWizardStep>

                <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">

                </asp:CompleteWizardStep>

            </WizardSteps>

            <SideBarStyle BackColor="#7C6F57" BorderWidth="0px" Font-Size="0.9em" VerticalAlign="Top" />

            <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />

            <SideBarButtonStyle BorderWidth="0px" Font-Names="Verdana" ForeColor="#FFFFFF" />

            <NavigationButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"

                BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />

            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" HorizontalAlign="Center" />

            <CreateUserButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"

                BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />

            <ContinueButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"

                BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />

            <StepStyle BorderWidth="0px" />

        </asp:CreateUserWizard>

 

Step-3: Create Login.aspx Page:-

    <h2>

        Login</h2>

    <p>

        <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid"

            BorderWidth="1px" CreateUserText="Don't have an account? Create one!" CreateUserUrl="~/CreateAccount.aspx"

            Font-Names="Verdana" Font-Size="10pt">

            <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />

        </asp:Login>

        </p>

Step-3: Create UserList.aspx Page:-

    <p>

        <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"

            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Font-Names="Verdana" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="allUsersDataSource" EmptyDataText="There are no users in the system..." Font-Italic="False">

            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />

            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

            <Columns>

                <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True" SortExpression="UserName" />

                <asp:TemplateField HeaderText="Email">

                    <ItemTemplate>

                        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Email", "mailto:{0}") %>'

                            Text='<%# Eval("Email") %>'></asp:HyperLink>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:BoundField DataField="CreationDate" HeaderText="Created On" ReadOnly="True"

                    SortExpression="CreationDate" />

                <asp:BoundField DataField="LastLoginDate" HeaderText="Last Login" SortExpression="LastLoginDate" />

            </Columns>

            <EmptyDataRowStyle Font-Italic="True" />

        </asp:GridView>

        <asp:ObjectDataSource ID="allUsersDataSource" runat="server" SelectMethod="GetAllUsers" TypeName="System.Web.Security.Membership"></asp:ObjectDataSource>

    </p>

Web Parts Implementation:

Now, we will implement web parts in this project, we no need to run again aspnet_regsql.exe tool for needed tables and also no need to customize again web.config file because Necessary tables used for web parts are already created when we first run aspnet_regsql.exe tool at start of this project. Even we have already customized web.config file earlier in step-1. Actually web part technique uses SqlMembershipProvider for storing information in database.

Used Tables by web parts mainly are:

  1. aspnet_Applications
  2. aspnet_Membership
  3. aspnet_Paths
  4. aspnet_PersonalizationPerUser

Now, we will create a custom control to use in WebPartSample.aspx page.

Name - DisplayModeMenu.ascx

<%@ Control Language="C#"

AutoEventWireup="true"

CodeFile="DisplayModeMenu.ascx.cs"

Inherits="DisplayModeMenu" %>

 

<div>

          <asp:Panel ID="Panel1" runat="server" BackColor="lightgray" BorderWidth="1" Font-Names="Verdana, Arial, Sans Serif"

                   Width="250">

                   <asp:Label ID="Label1" runat="server" Font-Bold="true" Font-Size="8" Text=" Display Mode"

                             Width="120"></asp:Label>

                   <asp:DropDownList ID="ddlDisplayMode" runat="server" AutoPostBack="true"

                             OnSelectedIndexChanged="ddlDisplayMode_SelectedIndexChanged" Width="120">

                   </asp:DropDownList>

          </asp:Panel>

</div>

Name - DisplayModeMenu.ascx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class DisplayModeMenu : System.Web.UI.UserControl

{

   // Use a field to reference the current WebPartManager control.

   WebPartManager webPartManager;

 

   public void Page_Init( object sender, EventArgs e )

   {

      Page.InitComplete += new EventHandler( InitComplete );

   }

 

   // when the page is fully initialized

   public void InitComplete( object sender, System.EventArgs e )

   {

      webPartManager = WebPartManager.GetCurrentWebPartManager( Page );

 

      String browseModeName = WebPartManager.BrowseDisplayMode.Name;

 

      foreach ( WebPartDisplayMode mode in

        webPartManager.SupportedDisplayModes )

      {

         String modeName = mode.Name;

         if ( mode.IsEnabled( webPartManager ) )

         {

            ListItem listItem = new ListItem( modeName, modeName );

            ddlDisplayMode.Items.Add( listItem );

         }

      }

   }

 

   // Change the page to the selected display mode.

   public void ddlDisplayMode_SelectedIndexChanged( object sender,

     EventArgs e )

   {

      String selectedMode = ddlDisplayMode.SelectedValue;

 

      WebPartDisplayMode mode =

       webPartManager.SupportedDisplayModes[selectedMode];

      if ( mode != null )

      {

         webPartManager.DisplayMode = mode;

      }

   }

 

   // Set the selected item equal to the current display mode.

   public void Page_PreRender( object sender, EventArgs e )

   {

      ListItemCollection items = ddlDisplayMode.Items;

      int selectedIndex =

        items.IndexOf( items.FindByText( webPartManager.DisplayMode.Name ) );

      ddlDisplayMode.SelectedIndex = selectedIndex;

   }

 

}

Step-Final: Create WebPartSample.aspx Page:-

    <div>

        &nbsp;<asp:WebPartManager ID="WebPartManager1" runat="server">

        </asp:WebPartManager>

        &nbsp;

        <uc1:DisplayModeMenu ID="DisplayModeMenu1" runat="server" />

        <br />

        <table width="50%">

            <tr>

                <td style="width: 25%; height: 174px;">

                    <asp:WebPartZone ID="WebPartZone1" runat="server" Height="69px" Width="168px" BorderColor="#CCCCCC"

                        Font-Names="Verdana" Padding="6">

                        <PartChromeStyle BackColor="#F7F6F3" BorderColor="#E2DED6" Font-Names="Verdana" ForeColor="White" />

                        <MenuLabelHoverStyle ForeColor="#E2DED6" />

                        <EmptyZoneTextStyle Font-Size="0.8em" />

                        <MenuLabelStyle ForeColor="White" />

                        <MenuVerbHoverStyle BackColor="#F7F6F3" BorderColor="#CCCCCC" BorderStyle="Solid"

                            BorderWidth="1px" ForeColor="#333333" />

                        <HeaderStyle Font-Size="0.7em" ForeColor="#CCCCCC" HorizontalAlign="Center" />

                        <MenuVerbStyle BorderColor="#5D7B9D" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />

                        <PartStyle Font-Size="0.8em" ForeColor="#333333" />

                        <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />

                        <MenuPopupStyle BackColor="#5D7B9D" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana"

                            Font-Size="0.6em" />

                        <PartTitleStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />

                        <ZoneTemplate>

                            <asp:Label ID="Label1" runat="server" Text="World is a place of play!"></asp:Label>

                        </ZoneTemplate>

                    </asp:WebPartZone>

                </td>

                <td style="width: 234px; height: 174px;">

                    <asp:WebPartZone ID="WebPartZone2" runat="server" BorderColor="#CCCCCC" Font-Names="Verdana"

                        Height="71px" Padding="6" Width="275px">

                        <PartChromeStyle BackColor="#F7F6F3" BorderColor="#E2DED6" Font-Names="Verdana" ForeColor="White" />

                        <MenuLabelHoverStyle ForeColor="#E2DED6" />

                        <EmptyZoneTextStyle Font-Size="0.8em" />

                        <MenuLabelStyle ForeColor="White" />

                        <MenuVerbHoverStyle BackColor="#F7F6F3" BorderColor="#CCCCCC" BorderStyle="Solid"

                            BorderWidth="1px" ForeColor="#333333" />

                        <HeaderStyle Font-Size="0.7em" ForeColor="#CCCCCC" HorizontalAlign="Center" />

                        <MenuVerbStyle BorderColor="#5D7B9D" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />

                        <PartStyle Font-Size="0.8em" ForeColor="#333333" />

                        <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />

                        <MenuPopupStyle BackColor="#5D7B9D" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana"

                            Font-Size="0.6em" />

                        <PartTitleStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />

                    </asp:WebPartZone>

                </td>

                <td style="width: 219px; height: 174px;">

                    <asp:WebPartZone ID="WebPartZone3" runat="server" BorderColor="#CCCCCC" Font-Names="Verdana"

                        Height="69px" Padding="6" Width="168px">

                        <PartChromeStyle BackColor="#F7F6F3" BorderColor="#E2DED6" Font-Names="Verdana" ForeColor="White" />

                        <MenuLabelHoverStyle ForeColor="#E2DED6" />

                        <EmptyZoneTextStyle Font-Size="0.8em" />

                        <MenuLabelStyle ForeColor="White" />

                        <MenuVerbHoverStyle BackColor="#F7F6F3" BorderColor="#CCCCCC" BorderStyle="Solid"

                            BorderWidth="1px" ForeColor="#333333" />

                        <HeaderStyle Font-Size="0.7em" ForeColor="#CCCCCC" HorizontalAlign="Center" />

                        <MenuVerbStyle BorderColor="#5D7B9D" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />

                        <PartStyle Font-Size="0.8em" ForeColor="#333333" />

                        <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />

                        <MenuPopupStyle BackColor="#5D7B9D" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana"

                            Font-Size="0.6em" />

                        <PartTitleStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />

                        <ZoneTemplate>

                            <asp:ListBox ID="ListBox1" runat="server" Width="192px">

                                <asp:ListItem>Apple Juce</asp:ListItem>

                                <asp:ListItem>Orange Juce</asp:ListItem>

                                <asp:ListItem>Banana Juce</asp:ListItem>

                                <asp:ListItem>Lemon Juce</asp:ListItem>

                            </asp:ListBox>

                        </ZoneTemplate>

                    </asp:WebPartZone>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:CatalogZone ID="CatalogZone1" runat="server" BackColor="#F7F6F3" BorderColor="#CCCCCC"

                        BorderWidth="1px" Font-Names="Verdana" Padding="6">

                        <HeaderVerbStyle Font-Bold="False" Font-Size="0.8em" Font-Underline="False" ForeColor="#333333" />

                        <PartTitleStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />

                        <PartChromeStyle BorderColor="#E2DED6" BorderStyle="Solid" BorderWidth="1px" />

                        <InstructionTextStyle Font-Size="0.8em" ForeColor="#333333" />

                        <PartLinkStyle Font-Size="0.8em" />

                        <EmptyZoneTextStyle Font-Size="0.8em" ForeColor="#333333" />

                        <LabelStyle Font-Size="0.8em" ForeColor="#333333" />

                        <VerbStyle Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" />

                        <ZoneTemplate>

                            <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">

                                <WebPartsTemplate>

                                    <asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999"

                                        CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"

                                        ForeColor="Black" Height="180px" Width="200px">

                                        <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />

                                        <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />

                                        <SelectorStyle BackColor="#CCCCCC" />

                                        <WeekendDayStyle BackColor="#FFFFCC" />

                                        <OtherMonthDayStyle ForeColor="#808080" />

                                        <NextPrevStyle VerticalAlign="Bottom" />

                                        <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />

                                        <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />

                                    </asp:Calendar>

                                    <asp:FileUpload ID="FileUpload1" runat="server" />

                                </WebPartsTemplate>

                            </asp:DeclarativeCatalogPart>

                            <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />

                        </ZoneTemplate>

                        <PartStyle BorderColor="#F7F6F3" BorderWidth="5px" />

                        <SelectedPartLinkStyle Font-Size="0.8em" />

                        <FooterStyle BackColor="#E2DED6" HorizontalAlign="Right" />

                        <HeaderStyle BackColor="#E2DED6" Font-Bold="True" Font-Size="0.8em" ForeColor="#333333" />

                        <EditUIStyle Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" />

                    </asp:CatalogZone>

                </td>

                <td style="width: 234px">

                    <asp:WebPartZone ID="WebPartZone5" runat="server" BorderColor="#CCCCCC" Font-Names="Verdana"

                        Height="69px" Padding="6" Width="275px">

                        <PartChromeStyle BackColor="#F7F6F3" BorderColor="#E2DED6" Font-Names="Verdana" ForeColor="White" />

                        <MenuLabelHoverStyle ForeColor="#E2DED6" />

                        <EmptyZoneTextStyle Font-Size="0.8em" />

                        <MenuLabelStyle ForeColor="White" />

                        <MenuVerbHoverStyle BackColor="#F7F6F3" BorderColor="#CCCCCC" BorderStyle="Solid"

                            BorderWidth="1px" ForeColor="#333333" />

                        <HeaderStyle Font-Size="0.7em" ForeColor="#CCCCCC" HorizontalAlign="Center" />

                        <MenuVerbStyle BorderColor="#5D7B9D" BorderStyle="Solid" BorderWidth="1px" ForeColor="White" />

                        <PartStyle Font-Size="0.8em" ForeColor="#333333" />

                        <TitleBarVerbStyle Font-Size="0.6em" Font-Underline="False" ForeColor="White" />

                        <MenuPopupStyle BackColor="#5D7B9D" BorderColor="#CCCCCC" BorderWidth="1px" Font-Names="Verdana"

                            Font-Size="0.6em" />

                        <PartTitleStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.8em" ForeColor="White" />

                    </asp:WebPartZone>

                </td>

                <td style="width: 219px">

                    &nbsp;<asp:EditorZone ID="EditorZone1" runat="server" BackColor="#F7F6F3" BorderColor="#CCCCCC"

                        BorderWidth="1px" Font-Names="Verdana" Padding="6">

                        <HeaderStyle BackColor="#E2DED6" Font-Bold="True" Font-Size="0.8em" ForeColor="#333333" />

                        <LabelStyle Font-Size="0.8em" ForeColor="#333333" />

                        <HeaderVerbStyle Font-Bold="False" Font-Size="0.8em" Font-Underline="False" ForeColor="#333333" />

                        <PartChromeStyle BorderColor="#E2DED6" BorderStyle="Solid" BorderWidth="1px" />

                        <PartStyle BorderColor="#F7F6F3" BorderWidth="5px" />

                        <FooterStyle BackColor="#E2DED6" HorizontalAlign="Right" />

                        <EditUIStyle Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" />

                        <InstructionTextStyle Font-Size="0.8em" ForeColor="#333333" />

                        <ErrorStyle Font-Size="0.8em" />

                        <VerbStyle Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" />

                        <EmptyZoneTextStyle Font-Size="0.8em" ForeColor="#333333" />

                        <PartTitleStyle Font-Bold="True" Font-Size="0.8em" ForeColor="#333333" />

                        <ZoneTemplate>

                            <asp:AppearanceEditorPart ID="AppearanceEditorPart1" runat="server" />

                        </ZoneTemplate>

                    </asp:EditorZone>

                </td>

            </tr>

        </table>

    </div>

Here, Link of sample project for details - http://www.eggheadcafe.com/fileupload/1639792319_WebPartsDemo.zip

By Jakir Hossain   Popularity  (4360 Views)