Saturday, 19 May 2012

Login Control in Asp.net

                                       login control in asp.net


 first create default.aspx page and take login control and create userpage.aspx,register.aspx,help.aspx

Log In
Please enter your user name and password for login.
Register
Additional Help
Label
code behind 
coding for this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
            ViewState["LoginErrors"] = 0;
    }

   
    protected void Login1_LoginError(object sender, EventArgs e)
    {
        if (ViewState["LoginErrors"] == null)
            ViewState["LoginErrors"] = 0;

        int ErrorCount = (int)ViewState["LoginErrors"] + 1;
        ViewState["LoginErrors"] = ErrorCount;

        if ((ErrorCount > 3) && (Login1.PasswordRecoveryUrl != string.Empty))
            Response.Redirect(Login1.PasswordRecoveryUrl);
    }

    private bool YourValidationFunction(string UserName, string Password)
    {
        bool boolReturnValue = false;
        string strConnection = "database=yogi; data source=USER-;integrated security=true";
        SqlConnection sqlConnection = new SqlConnection(strConnection);
        String SQLQuery = "SELECT name, password FROM emp1";
        SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
        SqlDataReader Dr;
        sqlConnection.Open();
        Dr = command.ExecuteReader();
        while (Dr.Read())
        {
            if ((UserName == Dr["name"].ToString()) & (Password == Dr["password"].ToString()))
            {
                boolReturnValue = true;
            }
            Dr.Close();
            return boolReturnValue;
        }
        return boolReturnValue;
    }
    protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e)
    {
        if (YourValidationFunction(Login1.UserName, Login1.Password))
        {
            // e.Authenticated = true;
            Login1.Visible = false;
            Response.Redirect("userpage.aspx");
            Label1.Text = "Successfully Logged In";
        }
        else
        {
            e.Authenticated = false;
        }
    }
}
html coding:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.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>Untitled Page</title>
    <style type="text/css">
 .LoginControl
{
      background-color:#F7F7DE;
      border-color:#CCCC99;
      border-style:solid;
    border-width:1px;
    font-family:Verdana;
    font-size:10px;    
}
    
    
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Login ID="Login1" runat="server" CssClass="LoginControl"
CreateUserText="Register"
CreateUserUrl="Register.aspx"
HelpPageText="Additional Help" HelpPageUrl="Help.aspx"
InstructionText="Please enter your user name and password for login." 
            BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4" BorderStyle="Solid" 
            BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" 
            onauthenticate="Login1_Authenticate1">
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" ForeColor="#FFFFFF" 
            Font-Size="0.9em" />
        <TextBoxStyle Font-Size="0.8em" />
        <LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" 
            BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
        <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
</asp:Login>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>




No comments:

Post a Comment