Showing posts with label ASP.NET Membership Provider. Show all posts
Showing posts with label ASP.NET Membership Provider. Show all posts

Tuesday, January 11, 2011

Creating & using the SQL Membership Provider in ASP.NET 2

This is a continuation of the previous blog Creating & using the SQL Membership Provider in ASP.NET 1.
In this post we shall cover the use of the Login controls in ASP.NET. They are:-
  1. Login
  2. LoginView
  3. PasswordRecovery
  4. LoginStatus
  5. LoginName
  6. CreateUserWizard
  7. ChangePassword
The default Login Page.
ASP.NET requires that we have a default login page. There are two ways of assigning this:-
  • Name a page as Login.aspx
  • Add the following code to the web.config file.
<authentication mode="Forms">
            <forms loginUrl="mylogin.aspx" />
        </authentication>

This will assign mylogin.aspx as the default login page, instead of Login.aspx.



The Login control.

The Login control is used for logging in an user to a ASP.NET website. Be careful not to create the Authenticate event, in which case you have to perform the authentication through code(A part which we shall cover next).We need to set the DestinationPageUrl to which the user shall be directed on successful Login.
Here is the code for the Login Page.
Login.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Login.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Welcome.aspx">
        </asp:Login>
   
    </div>
    </form>
</body>
</html>


Login.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
  
}





The CreateUserWizard is used to create a new user.
Registration.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Registration Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
            <WizardSteps>
                <asp:CreateUserWizardStep runat="server" />
                <asp:CompleteWizardStep runat="server" />
            </WizardSteps>
        </asp:CreateUserWizard>
   
    </div>
    </form>
</body>
</html>



Registration.aspx.cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}





The LoginView control contains two templates an Anonymous Template for showing content to unauthenticated users, and a LoggedIn Template for showing content to LoggedIn users. In addition we can have templates for different roles(a topic we shall cover in the future). I have placed a LoginView control on the welcome.aspx page. In the Logged In template I have placed a ChangePassword control for changing an existing password. In the anonymous template I have placed the words "Not Logged In". I have also placed a LoginStatus control on the page. This will display an option for Login if no user is logged in.It will display Logout otherwise.
 Here is the code for Welcome.aspx


Welcome.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Welcome.aspx.cs" Inherits="Welcome" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:LoginView ID="LoginView1" runat="server">
        <LoggedInTemplate>
            <asp:ChangePassword ID="ChangePassword1" runat="server">
            </asp:ChangePassword>
        </LoggedInTemplate>
        <AnonymousTemplate>
            Not Logged In
        </AnonymousTemplate>
    </asp:LoginView>
    <asp:LoginStatus ID="LoginStatus1" runat="server" />
    <asp:LoginName ID="LoginName1" runat="server" />
    </form>
</body>
</html>


Welcome.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}




The PasswordRecovery control requires  configuration of smtp server. Place the following code in the web.config file

<system.net>
        <mailSettings >
            <smtp>
                <network host="localhost" port="25"   defaultCredentials ="true"/>
            </smtp>
        </mailSettings>
       
    </system.net>



    The password recovery control requires an EMail account to be configured. Here is the code:-
<asp:PasswordRecovery ID="PasswordRecovery1"
            style="position:absolute; top: 282px; left: 280px;" runat="server">
            <MailDefinition From="webmaster@supermail.com">
            </MailDefinition>
        </asp:PasswordRecovery>






Here is the code for the PasswordRecovery.aspx page that contains a PasswordRecovery Control.
PasswordRecovery.aspx
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PasswordRecovery.aspx.cs" Inherits="PasswordRecovery" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Password Recovery Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
    </div>
    <asp:PasswordRecovery ID="PasswordRecovery1" runat="server">
        <MailDefinition From="webmaster@supermail.com">
        </MailDefinition>
    </asp:PasswordRecovery>
    </form>
</body>
</html>

 The Registration Page in action:-

The Login Page in action:-

The Welcome Page after Login:-
The PasswordRecovery Page in action:-

In a future post we shall use the Roles API, and also use the Membership API through code.

Saturday, January 8, 2011

Creating & using the SQL Membership Provider in ASP.NET 1

The SQL Membership Provider provides a easy to use,highly customizable and comprehensive framework for creating and managing the membership & role management system for a web site. It provides both a Web Control based framework, and also a programmatic API. To start we need to create a SQL Server database. Here is how you do it ---
  • Create a new Website
  • Open View/Server Explorer
  • Right Click on Data Connections
  • Select Create New SQL Server Database
  • Enter the name of location & name of the Server, then the name of the database.
  • Then press OK.




Now we shall use the aspnet_regsql tool that ships with Visual Studio tools, to configure the newly created database for membership use.

 Here is how you do it ---


Here is the configured database.

Now we shall register our Membership Provider in the machine.config XML File that is to be found under 
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
Replace C:\Windows with the path of your windows installation.

add the following code in the Connection Strings section:
    <add name="NewMembershipDB" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=MemberShipDB" providerName="System.Data.SqlClient"/>

Add the new MemberShip Provider in the membership/providers section
<add name="NewMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="NewMembershipDB" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="true" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="2" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>





Use the ASP.Net Configuration tool to configure the membership provider for your website. Select the membership provider that we prepared before:-
Select the Provider Tab and select our membership provider NewMembershipProvider which should be visible there 


Then, open the security tab and select authentication type to: From the Internet
The membership provider is now ready. In the next post we shall use the membership controls in ASP.NET.
This is the link to the next post in the series:-