Wednesday, November 19, 2008

Asp.net 2.0 login controls C# Access database sample code

Asp.net 2.0 login controls C# Access database sample code with connectionstring
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!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>
                 <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate"
        BackColor="Transparent" BorderColor="#ffccff" BorderPadding="4" BorderStyle="Solid"
        BorderWidth="0px" Font-Names="Verdana" Font-Size="10px" ForeColor="#333333"
        Height="53px" Width="100%"  >
            <TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White"  />
            <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
            <TextBoxStyle Font-Size="1.2em" />
            <LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px"
                Font-Names="Verdana" Font-Size="12px" ForeColor="#284E98" />
            <LayoutTemplate>
                <table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse; height: 99px;">
                    <tr>
                        <td  align="left">
                            <table border="0" cellpadding="0" style="width: 100%; height: 25%">
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" Font-Bold="True" ForeColor="White" Font-Size="10px">User Name:</asp:Label></td>
                                    <td align="left">
                                        <asp:TextBox ID="UserName" runat="server" Font-Size="10px" BackColor="#FFFFC0" Font-Bold="False"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                            ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" style="height: 23px">
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" Font-Bold="True" ForeColor="White" Font-Size="10px">Password:</asp:Label></td>
                                    <td align="left" style="height: 23px">
                                        <asp:TextBox ID="Password" runat="server" Font-Size="10px" TextMode="Password" BackColor="#FFFFC0" Font-Bold="False"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                            ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                        <asp:Button ID="LoginButton" runat="server" BackColor="White" BorderColor="#507CD1"
                                            BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
                                            Font-Size="11px" ForeColor="#284E98" Text="Log In" ValidationGroup="Login1" OnClick="LoginButton_Click" />
                                           
                                            </td>
                                </tr>
                                <tr>
                                    <td colspan="2" align="left">
                                        <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me" Font-Bold="False" ForeColor="White"  />
                                        </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2" style="color: red; height: 16px;">
                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                    </td>
                                </tr>
                            </table>
                            <strong><span style="color: #ffff33"></span></strong></td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:Login>
   
    </div>
    </form>
</body>
</html>

-----
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;
using System.Data.OleDb;
public partial class login : System.Web.UI.Page
{
    string CaseIDClient;  
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool Authenticated = false;
        Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
        e.Authenticated = Authenticated;
        if (Authenticated == true)
        {
           Response.Redirect("dispcase.aspx");
/*
            if (Session["userRole"].ToString() == "client")
            {
                Response.Redirect("clientcasestatus.aspx?caseID=" + CaseIDClient);
            }
            else
            {
                Response.Redirect("dispcase.aspx");
            }
 */
        }
    }
    private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
    {
        OleDbConnection conDatabase = null;
        OleDbDataReader Dr = null;
        bool boolReturnValue = false;
        string strUserID = UserName;
        conDatabase = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\database\\testDB.mdb");
        conDatabase.Open();
        string strSQL = "select * from USERS where USERID=\'" + (strUserID + "\'");
        OleDbCommand cmdDatabase = new OleDbCommand(strSQL, conDatabase);
        Dr = cmdDatabase.ExecuteReader();
        while (Dr.Read())
        {
            string UserNameDB = Dr["USERID"].ToString();
            string PasswordDB = Dr["PASSWORD"].ToString();
            string userbranchDB = Dr["USERBRANCH"].ToString();
            string userRoleDB = Dr["ROLE"].ToString();
           

            if ((UserName == UserNameDB.TrimEnd().TrimStart()) & (Password == PasswordDB.TrimEnd().TrimStart()))
            {
                Session["loginm"] = strUserID;
                Session["userRole"] = userRoleDB;
                Session["userBranch"] = userbranchDB;
                Session["CaseIDClient"] = Dr["CASENO"].ToString();
                boolReturnValue = true;
                break;

            }
           
        }
        conDatabase.Close();
        Dr.Close();
        return boolReturnValue;
    }
   
    protected void LoginButton_Click(object sender, EventArgs e)
    {
    }
}

 

1 comment:

Unknown said...

i want coding Login button click
all checking done in that