Wednesday, February 9, 2011

Simple Login control c# asp.net 4.0 from Database

 <form id="form1" runat="server">
 
        <table style="width:100%" bgcolor="White"><tr>
        <td style="height: 17px">
            &nbsp; &nbsp;&nbsp;
            </td>
            <td style="height: 17px">
                <table align="center" border="0"  cellpadding="0" cellspacing="0"
                    style="vertical-align: middle">
                    <tr>
                        <td align="center" style="height: 186px" valign="baseline">
                            <table align="center" border="0" cellpadding="0" cellspacing="0" style="width: 96%;
                                height: 50%; background-color: white">
                                <tr>
                                    <td align="right"style="height: 169px">
                                        <asp:Login ID="Login1" runat="server" BackColor="LightBlue" BorderColor="#B5C7DE"
                                            BorderPadding="4" BorderStyle="Solid" BorderWidth="0px" Font-Names="Verdana"
                                            Font-Size="0.8em" ForeColor="#333333" Height="10%" OnAuthenticate="Login1_Authenticate"
                                            Width="68%">
                                            <TextBoxStyle Font-Size="0.8em" />
                                            <LoginButtonStyle BackColor="White" BorderColor="Lavender" BorderStyle="Solid" BorderWidth="0px"
                                                Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
                                            <LayoutTemplate>
                                                <table bgcolor="#e5dbcf" border="0" cellpadding="4" cellspacing="0" style="width: 319px;
                                                    border-collapse: collapse">
                                                    <tr>
                                                        <td>
                                                            <table border="0" cellpadding="0" style="width: 100%; height: 5%">
                                                                <tr>
                                                                    <td align="right">
                                                                        <br />
                                                                    </td>
                                                                    <td align="left">
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="right">
                                                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" Font-Bold="True"
                                                                            ForeColor="#217C7E">User&nbsp;Name:</asp:Label></td>
                                                                    <td align="left">
                                                                        <asp:TextBox ID="UserName" runat="server" BackColor="AliceBlue" Font-Size="11px"
                                                                            Height="12px" Width="117px"></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">
                                                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" Font-Bold="True"
                                                                            ForeColor="#217C7E">Password:</asp:Label></td>
                                                                    <td align="left">
                                                                        <asp:TextBox ID="Password" runat="server" BackColor="AliceBlue" Font-Size="11px"
                                                                            Height="12px" TextMode="Password" Width="117px"></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="#217C7E" BorderColor="#C0C0FF"
                                                                            BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Bold="True" Font-Names="Verdana"
                                                                            Font-Size="10px" ForeColor="Lavender" OnClick="LoginButton_Click" Text="Log In"
                                                                            ValidationGroup="Login1" /></td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="left" colspan="2" valign="bottom">
                                                                        &nbsp; &nbsp; &nbsp;
                                                                        <asp:CheckBox ID="RememberMe" runat="server" Font-Bold="False" ForeColor="#217C7E"
                                                                            Text="Remember me" />&nbsp;<a href="forpasswd.aspx" style="text-decoration:none"><font color="#217C7E"> Forgot your password?</font></a></td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="center" colspan="2" style="color: red; height: 12px">
                                                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="right" colspan="2">
                                                                        &nbsp;</td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </LayoutTemplate>
                                            <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
                                            <TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
                                        </asp:Login>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        </table>
        </form>

 

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.SqlClient;

 

public partial class login : System.Web.UI.Page
{
    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("usresult.aspx");
        }

    }
    private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
    {

        bool boolReturnValue = false;

        string strConnection = ConnectionString;

        SqlConnection Connection = new SqlConnection(strConnection);
        string strUserID = UserName;
        String strSQL = "select * from UserDetails where UDUSRID=\'" + (strUserID + "\'");
        SqlCommand command = new SqlCommand(strSQL, Connection);
        SqlDataReader Dr;
        Connection.Open();
        Dr = command.ExecuteReader();
        while (Dr.Read())
        {
            string UserNameDB = Dr["UDUSRID"].ToString();
            string PasswordDB = Dr["UDPASSWD"].ToString();
            //  string UserRoleDB = Dr["UDROLE"].ToString();


            if ((UserName == UserNameDB.TrimEnd().TrimStart()) & (Password == PasswordDB.TrimEnd().TrimStart()))
            {
                Session["Usrid"] = UserNameDB;
                Session["Uspasswd"] = PasswordDB;
                //    Session["Usrole"] = UserRoleDB;
                //  Response.Write(Session["Usrole"]);
                // Response.End();

                boolReturnValue = true;
                break;

            }

        }

        Dr.Close();

        return boolReturnValue;

    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {

    }

    private string ConnectionString
    {
        get
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
            return connectionString;
        }
    }
}


 

No comments: